Search code examples
shellbatch-filecygwin

how to pass a parameter containing & to a bat file from cygwin bash


Here's a simple bat file:

C:\temp>type a.bat
@echo off
rem try
echo %1

With some difficulty, I am able to pass a&b as parameter to it:

C:\temp>a.bat a&b
a
'b' is not recognized as an internal or external command,
operable program or batch file.
C:\temp>a.bat "a&b"
"a&b"

The paramater has the " character; I can live with it. But I can't figure out how to call it from a cygwin shell:

C:\temp>c:\cygwin\bin\sh
sh-4.1$ ./a.bat a&b
[1] 7760
a
sh: b: command not found
sh-4.1$ ./a.bat a\&b
a
'b' is not recognized as an internal or external command,
operable program or batch file.
[1]+  Done                    ./a.bat a
sh-4.1$ ./a.bat \"a\&b\"
"\"a
'b\""' is not recognized as an internal or external command,
operable program or batch file.
sh-4.1$ ./a.bat "a\&b"
a\
'b' is not recognized as an internal or external command,
operable program or batch file.

Solution

  • Windows CMD uses ^ to escape most special characters. So you can use that to pass your argument without enclosing quotes.

    C:\temp>a.bat a^&b
    

    But the parameter that is received will be a&b. Your batch script will give an error when it attempts to ECHO the value of %1 because the & is not quoted or escaped.

    You could safely echo the value if you enclose it in quotes:

    echo "%1"
    

    But if you pass the value already enclosed in quotes "a&b", then you get an error again. That is why many batch script use the ~ modifier to remove any existing enclosing quotes (if they exist), and then explicitly add quotes. The following will work if the value of %1 is quoted or unquoted.

    echo "%~1"
    

    You still can have problems with something like "a&b"&c, but that is another story :-)

    Another option would be to double escape the & in the original command line:

    C:\temp>a.bat a^^^&b
    

    Your batch script will recieve a^&b, and then the echo will work.

    With regard to Cygwin, I know very little, but I believe I mostly understand tests 1, 2 and 3.

    Test 1:

    sh-4.1$ ./a.bat a&b
    [1] 7760
    a
    sh: b: command not found
    

    Cygwin is passing a to your batch script and failing to execute command b

    Test 2:

    sh-4.1$ ./a.bat a\&b
    a
    'b' is not recognized as an internal or external command, operable program or batch file.
    

    I'm not sure if CMD parses the command line passed from Cygwin before the batch script is executed or not.

    If so, then a is getting passed to your batch script, and then CMD.EXE is failing to execute b.

    If not, then Cygwin is successfully executing your script and passing a&b, but your ECHO statement is failing as I explained earlier.

    One of the following should work with your script, but I'm not sure which:

    sh-4.1$ ./a.bat a^\&b
    or
    sh-4.1$ ./a.bat a^^^\&b
    

    One of the above will pass a^&b and your ECHO should work.

    Test 3:

    sh-4.1$ ./a.bat \"a\&b\"
    "\"a
    'b\""' is not recognized as an internal or external command, operable program or batch file.
    

    I have no idea what Cygwin is doing. Somehow it is introducing additional double quotes.

    Test 4:

    sh-4.1$ ./a.bat "a\&b"
    a\
    'b' is not recognized as an internal or external command, operable program or batch file.
    

    Cygwin strips the quotes, and the backslash is preserved. Again, either the & is causing problems when CMD.EXE is launching the script, or it is causing problems within the script when you ECHO it.

    One of the following should work, passing a^&b to your script. I'm just not sure which one:

    sh-4.1$ ./a.bat "a^&b"
    or
    sh-4.1$ ./a.bat "a^^^&b"
    

    I believe the following will successfully pass "a&b" to your script:

    sh-4.1$ ./a.bat '"a&b"'
    

    I also think the following will do the same, though I am not as confident due to the result of Test 3.

    sh-4.1$ ./a.bat "\"a&b\""