jeb
jeb

Reputation: 82307

Proper escaping quotes from bash to batch

I'm trying to start from a cygwin-bash context a batch process.
But my quotes are removed or escaped with backslashes.

Sample:

npp="\"$(cygpath -w "/cygdrive/c/Program Files (x86)/Notepad++/notepad++.exe")\""
echo "$npp"
echo cmd /c "echo start \"\" $npp"
cmd /c "echo start \"\" $npp"

Output:

"C:\Program Files (x86)\Notepad++\notepad++.exe"
cmd /c echo start "" "C:\Program Files (x86)\Notepad++\notepad++.exe"
start \"\" \"C:\Program Files (x86)\Notepad++\notepad++.exe\"

The first and second line is the expected output.

But in the third line there are unwanted backslashes.
I suppose that the bash shell added these slashes always, but later they are removed by the bash shell again, but in the case of a batch-cmd context there is no process to remove the backslashes.

My question is, how to avoid the backslashes or how to remove them in the batch context?

Upvotes: 0

Views: 283

Answers (3)

Doug Henderson
Doug Henderson

Reputation: 898

To start NPP in notepad mode with foo.txt opened.

Use the cygpath --dos option, and the cygstart --verbose option:

cygstart --verbose --wait $(cygpath --dos "/cygdrive/c/Program Files (x86)/Notepad++/notepad++.exe") -multiInst -nosession foo.txt

or let cygstart do the cygpath for us:

cygstart --verbose --wait "/cygdrive/c/Program Files (x86)/Notepad++/notepad++.exe" -multiInst -nosession foo.txt

or just use cmd if it is in your path, otherwise:

/cygdrive/c/Windows/system32/cmd.exe /c start '""' /I /WAIT 'c:\Program Files (x86)\Notepad++\notepad++.exe' -multiInst -nosession -noPlugin -notabbar foo.txt

Upvotes: 1

jeb
jeb

Reputation: 82307

I found one solution, but it seems to be a bit ugly.

npp="$(cygpath -w "/cygdrive/c/Program Files (x86)/Notepad++/notepad++.exe")"
cmd /c "set q=\"\" & (call set q=%q:~1,1%) &  call echo start %q%%q% %q%$npp%q%"

The second line simply creates in the variable q a single quote sign.
First q is set to \"\" and the (call set q=%q:~1,1%) sets q with the second character of the previous content of q.

And the call ...%q% uses these quotes.

But as said before, this seems not to be a desirable solution.

Upvotes: 0

MC ND
MC ND

Reputation: 70923

I don't have a bash instance at hand, but tested on busybox (I know, not the same, just a test)

W:/ $ word='"C:\Program Files (x86)\Microsoft Office\Office14\WINWORD.EXE"'
W:/ $ eval "cmd /c echo start \"\" $word"
start "" "C:\Program Files (x86)\Microsoft Office\Office14\WINWORD.EXE"

W:/ $ word="C:\Program Files (x86)\Microsoft Office\Office14\WINWORD.EXE"
W:/ $ eval "cmd /c echo start \"\" \"$word\""
start "" "C:\Program Files (x86)\Microsoft Office\Office14\WINWORD.EXE"

Upvotes: 1

Related Questions