简体   繁体   中英

Batch Script - Escaping Double Quotes in variable

I want to run a script which only purpose is to execute its first argument, so I have the following script:

set command=%~1
%command%

And I run it like RunCommand.bat "echo hello world" which works.

Now I want to escape any special char, for example double quote. I tried a couple of options but non works. Any ideas?

This is my closest: RunCommand.bat "echo ""special char""" which prints-> ""special char""

EDIT

This script works

set "command=%~1"
set "command=%command:""="%"
%command%

refferenced from Escaping Double Quotes in Batch Script

No need to escape anything.

runcommand.bat:

@%*

(yes, that's all)

Output:

C:\temp>runcommand.bat echo "hello World!"
"hello World!"

C:\temp>runcommand.bat ping -n 1 www.google.com

Ping wird ausgeführt für www.google.com [216.58.205.228] mit 32 Bytes Daten:
Antwort von 216.58.205.228: Bytes=32 Zeit=13ms TTL=57

Ping-Statistik für 216.58.205.228:
    Pakete: Gesendet = 1, Empfangen = 1, Verloren = 0
    (0% Verlust),
Ca. Zeitangaben in Millisek.:
    Minimum = 13ms, Maximum = 13ms, Mittelwert = 13ms

C:\temp>runcommand.bat wmic os get serialnumber /value


SerialNumber=00248-80000-00000-AR7GX

In response to my bat file must receive its variable inside a double quote : I see no benefit in complicating things with additional quotes, when not needed, but here you go (still no escaping needed):

@cmd /c %*

Output:

C:\temp>runcommand "echo "special char"&echo hello"
"special char"
hello

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM