AlainD
AlainD

Reputation: 6607

Display exclamation marks (!) in command-line script with EnableDelayedExpansion

The output of this batch script is Hello! as expected:

@echo off
echo Hello!

However, with Delayed Expansion enabled the output is now Hello:

@echo off
setlocal EnableDelayedExpansion
echo Hello!

A trick is to insert endlocal just before the echo:

@echo off
setlocal EnableDelayedExpansion
...
endlocal
echo Hello!
setlocal EnableDelayedExpansion

This is tedious, however. According to this Microsoft reference, some charaters like percent (%) and caret (^) can be used to escape (and therefore print) characters such as %, | and >.

Is there any escape character for the exclamation mark (!) without resorting to the endlocal trick?

Upvotes: 1

Views: 1058

Answers (2)

T3RR0R
T3RR0R

Reputation: 2951

one can take @npocmaka's answer a step further, so that exclamation marks can be dispalyed without expanding in later use.

@Echo Off & setlocal DisableDelayedExpansion
Set "/Ex=^^!"

Setlocal EnableDelayedExpansion
Echo(%/Ex%test%/Ex%

enter image description here

Upvotes: 0

npocmaka
npocmaka

Reputation: 57252

try with double caret:

@echo off

setlocal enableDelayedExpansion

echo ^^!

Upvotes: 3

Related Questions