Search code examples
node.jsbashshellspawnspawning

How to pass & retrieve arguments to a .sh file in nodejs?


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?


Solution

  • 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 :)