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?
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\
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With