1

I want to create a bash script with a "pure" UNC (i.e. I do not want to escape the backslashes literally) as an argument, i.e. something like:

./foo \\my\share\is\here

However, as is natural, the above script reads:

\myshareishere

Can I somehow read the exact UNC as an argument and store it in a variable?

  • Git Bash allows you to use //my/share/is/here style UNC paths. You can even cd there (something a DOS prompt wouldn't let you). Might work with Cygwin too, but I haven't tried. – janos Jan 15 '15 at 23:14
3

Use single quotes:

[root@xxx601 ~]# ./stack \\my\share\is\here
You Gave Me: \myshareishere
[root@xxx601 ~]# ./stack '\\my\share\is\here'
You Gave Me: \\my\share\is\here
| improve this answer | |
  • In other words there is no way to avoid quotes, isn't it? – trikelef Jan 16 '15 at 8:28
  • Not really. It's the shell you're typing into that's interpreting the backslashes as escape characters so you can't really change the script you're running to get around that (it's already fubared by the time your script gets it). – Bratchley Jan 16 '15 at 14:57
2

Try: ./foo \\\\my\\share\\is\\here

Or: ./foo \\my\\share\\is\\here

It depends if you want double '\' at the begining or not.

| improve this answer | |
2

If you want to run the command ./foo with the argument \\my\share\is\here in a shell script, then you need to use some form of quoting, because the backslash character has a special meaning of the shell. The simplest form of quoting is to put single quotes around the argument: everything between a pair of single quotes is treated literally, the only character you can't include this way is the single quote character itself.

./foo '\\my\share\is\here'

If your script is foo and you want to run it with the argument \\my\share\is\here on the command line, then there too you need to use quoting (the command line is a shell).

If you type ./foo \\my\share\is\here, then foo has no way to know what you typed. It's the shell that runs foo that parses the command line, and decides that the space after ./foo is a word separator, and that the first word is a command name to execute and the second word is an argument, and that character sequences of the form backslash+something stand for the character something. It's just the same phenomenon as if you write foo ; bar: this is a shell command to run foo, then bar; it doesn't pass the string ; bar to foo. Similarly, ./foo \\my\share\is\here runs ./foo with the argument \myshareishere. If you want to run ./foo with the argument \\my\share\is\here, you need to write what you mean, for example ./foo '\\my\share\is\here' (or ./foo \\\\my\\share\\is\\here or ./foo '\'"\\m"y\\share'\is\'"here""" or any other variation).

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.