Reputation: 4154
How can I get the raw command line arguments in a node.js app, given this example command:
node forwardwithssh.js echo "hello arguments"
process.argv
will be [ "node", "/path/to/forwardwith.js", "echo", "hello arguments" ]
And there's no way to reconstruct the original echo "hello arguments"
from that (ie. join(" "
won't put quotes back).
I want the whole original raw string after the script name.
what I want is easily obtained in bash scripts with "$*"
, is there any equivalent way to get that in node.js?
Note: the intention in particular is to receive a command to be executed somewhere else (eg. thru ssh)
Upvotes: 12
Views: 3991
Reputation: 141887
Wrap each of the args in single quotes, and escape and single quotes within each arg to '\''
:
var cmd_string = process.argv.map( function(arg){
return "'" + arg.replace(/'/g, "'\\''") + "'";
}).join(' ');
That would give you a cmd_string
containing:
'node' '/path/to/forwardwith.js' 'echo' 'hello arguments'
which can be run directly in another shell.
Upvotes: 4