Home > Enterprise >  Interactive Command Prompt character escaping %
Interactive Command Prompt character escaping %

Time:03-16

I need to escape the % character in interactive Command Prompt (NOT in a batch file). How do I echo the text a %pathext% b; %pathext% expands to the environment variable; ^, %, \ and "quotes" all do not work. Please base the answer on: echo a %pathext% b.

Many thanks in advance.

CodePudding user response:

echo a %^pathext% b

Use a caret inside the variable (it can be anywhere inside the % signs) to prevent the parser from recognizing and substituting the variable.

CodePudding user response:

There is one simple solution, but it's not 100% bullet proof

echo a %path^ext% b

The position of the caret can be moved to any position. This works, because the variable expansion on the command line works different than in batch files.
If an undefined variable should be expanded, in a batch file it results in an empty text, but on the cli the percent expression will be used unchaged. The caret will be removed in a later step of the parser.

But it can still fail when there exists a variable named path^ext

For a bullet proof solution, you need to create a percent sign and expand it.

for /F "delims==" %# in ("%=%=") do echo a %#pathexit%# b
  • Related