Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Make a script which accept command-line arguments

What is the correct syntax for running a Node.js script with command-line arguments on Linux or Mac?

To run the script with no arguments, I would simply use the command node stuff.js, but in this case, I'd like to run a script called stuff.js with the arguments "blah", "hee", "woohoo!".

like image 990
Anderson Green Avatar asked Oct 17 '12 01:10

Anderson Green


People also ask

How can you pass command line arguments to a script?

Arguments can be passed to the script when it is executed, by writing them as a space-delimited list following the script file name. Inside the script, the $1 variable references the first argument in the command line, $2 the second argument and so forth.

What is command line argument in shell script?

Command line arguments (also known as positional parameters) are the arguments specified at the command prompt with a command or script to be executed. The locations at the command prompt of the arguments as well as the location of the command, or the script itself, are stored in corresponding variables.

Which shell script code will create files for all command line arguments?

Instead, we use the special variable $@ , which means, 'All of the command-line arguments to the shell script'. We also should put $@ inside double-quotes to handle the case of arguments containing spaces ( "$@" is special syntax and is equivalent to "$1" "$2" …).


1 Answers

See http://nodejs.org/docs/latest/api/process.html#process_process_argv

In summary you'll run it like

node stuff.js blah hee "whoohoo!"

Then your arguments are available in process.argv

like image 169
hexist Avatar answered Sep 30 '22 20:09

hexist