Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Escaping a batch file echo that starts with a forward slash and question mark

Bit of a tricky one. How can I correctly escape the following in a batch file?

echo    /?   display this help text

This particular combination of characters is treated as an "ECHO /?" command:

C:\Batch>ECHO       /?    display this help text
Displays messages, or turns command-echoing on or off.

  ECHO [ON | OFF]
  ECHO [message]

Type ECHO without parameters to display the current echo setting.

It does not respond to caret (^) escaping, ie. I've tried ^/? /^? and ^/^?.

NB: As a workaround, I found that inserting other characters in between is enough to bypass the ECHO command line processor, eg:

echo ...   /?   display this help text

Still, this is not ideal and I wondered if there was a way to acheive the desired output, namely with /? at the start of the echoed message.

like image 791
Mykro Avatar asked Jun 29 '11 01:06

Mykro


People also ask

How do you escape special characters in echo?

The backslash (\) character is used to mark these special characters so that they are not interpreted by the shell, but passed on to the command being run (for example, echo ). So to output the string: (Assuming that the value of $X is 5): A quote is ", backslash is \, backtick is `.

How do I escape a character from a batch file?

In batch files, the percent sign may be "escaped" by using a double percent sign ( %% ). That way, a single percent sign will be used as literal within the command line, instead of being further interpreted.

What is @echo off in batch script?

When echo is turned off, the command prompt doesn't appear in the Command Prompt window. To display the command prompt again, type echo on. To prevent all commands in a batch file (including the echo off command) from displaying on the screen, on the first line of the batch file type: Copy. @echo off.

What does @echo off do?

The ECHO-OFF command suppresses echoing for the terminal attached to a specified process. The ECHO-ON command restores input echoing for a specified process. These commands are frequently used in Procs.


2 Answers

For escaping echo arguments exists many variants, like echo., echo:, echo=
But only echo( seems to be secure against any appended text.

These one fails, if files exists like echo, echo[, echo] or echo+

echo.
echo[
echo]
echo+

These one fails, if a file in the current directory exists named my.bat

echo\..\my.bat
echo:\..\my.bat
echo.\..\my.bat

These one fails independet of a file

echo/?
echo,/?
echo;/?

Only the echo( seems to be always safe against any content

like image 77
jeb Avatar answered Nov 07 '22 21:11

jeb


For escaping echo arguments, you can use the alternative syntax echo.:

echo./?
like image 5
Kerrek SB Avatar answered Nov 07 '22 22:11

Kerrek SB