Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Passing a node.js script an argument with a space on Windows

I'm running a node script from the command line on Windows and am trying to pass in a folder path that includes a space. When accessing this argument, via require modules or via the process.argv variable I don't seem to get what I would expect. For the following command:

node script.js "C:\path\to\folder\with a space\"

I seem to get the following value:

process.argv[2] = C:\path\to\folder\with a space\"

Notice the trailing " in the string. If the argument is passed without quotes, it obviously passes it as different arguments split on the space.

Am I doing something wrong, or is this a bug? And if it is a bug, is there a possible workaround?

like image 529
LoveAndCoding Avatar asked Oct 06 '22 17:10

LoveAndCoding


1 Answers

The trailing backslash escapes the quote which is then again implied by the shell (instead of aborting due to lack of a closing quote).

The fix is simply escaping that backslash with another backslash or omitting it altogether:

C:\Users\myself> python -c "import sys; print sys.argv[1]" "foo\bar\"
foo\bar"
C:\Users\myself> python -c "import sys; print sys.argv[1]" "foo\bar\\"
foo\bar\

Note that you may only escape the last backslash this way - any other backslash ins the string will not act as an escape character:

C:\Users\myself> python -c "import sys; print sys.argv[1]" "foo\\bar\\"
foo\\bar\
like image 148
ThiefMaster Avatar answered Oct 13 '22 01:10

ThiefMaster