1

Can we pass arguments to a .sh file in Nodejs, i am trying to Spawn a .sh file, and want to pass some arguments while spawning,

  var command = spawn(__dirname + "/import.sh", {
    var1: "abc"
  });

in the above command i am trying to spawn the file import.sh and also trying to pass arguments along with it, i don't know if it's the correct way
and how to retrieve the variable value in the import.sh file?

Amrinder Singh
  • 5,300
  • 12
  • 46
  • 88
  • Did you read [the fine manual](https://nodejs.org/api/child_process.html#child_process_child_process_spawn_command_args_options)? – robertklep Jul 03 '17 at 10:09

1 Answers1

2

Finally i got the answer:

var env = Object.create(process.env);
env.var1 = "abc";

var command = spawn(__dirname + "/import.sh", {
    env: env
});

and to retrieve this in import.sh simply do:

if [ ${var1} == "abc" ]
then
    // your code goes here
fi

that's it :)

Amrinder Singh
  • 5,300
  • 12
  • 46
  • 88