84

When I have setlocal ENABLEDELAYEDEXPANSION set in a cmd script is there any way I can escape a ! that I want to use as a parameter to a command?

@echo off
setlocal ENABLEDELAYEDEXPANSION
echo I want to go out with a bang!
echo I still want to go out with a bang^!

4 Answers 4

98

That's what I found (^^)

@echo off
setlocal ENABLEDELAYEDEXPANSION
echo I want to go out with a bang^^!
3
  • 3
    Btw, if you had to replace an exclamation mark inside a variable how would you do that?
    – crosenblum
    Commented Nov 12, 2012 at 1:04
  • 2
    @crosenblum I would recommend to ask this as an own question. Than it will be visbile for all SO members and most likely answered.
    – FrVaBe
    Commented Nov 12, 2012 at 8:44
  • 1
    This (nor the other solutions) will work if the exclamation mark is inside a variable that you set/use or pass on to a call-label, in which case I think you'll be out of luck.
    – Abel
    Commented Sep 28, 2017 at 3:57
78

An additional remark to the answer of FrVaBe.

Normally the ^^! works, but in quotes you only need ^! instead.

echo I want to go out with a bang^^!
echo He said "Bang^!"

This is a result of the escape mechanism of the batch parser.

First the parser parses a line and the caret escapes the next character, in this case it has an effect for &|<>()"<linefeed>, but only outside of quotes, as inside of the quotes all characters are "normal" and the caret itself has no effect.

With delayed expansion an extra parse step follows, there is the caret also an escape character for the next character, but only affects the ! and ^, and quotes are ignored in this parsing step. This extra step will be executed only, if there is at least one ! in the line.

setlocal DisableDelayedExpansion
echo DisableDelayedExpansion
echo one caret^^
echo one caret^^  bang! "boom^!"

echo(
setlocal EnableDelayedExpansion
echo EnableDelayedExpansion
echo one caret^^
echo none caret^^  bang^^! "boom^!"

---- OUTPUT ------

DisableDelayedExpansion
one caret^
one caret^  bang! "boom^!"

EnableDelayedExpansion
one caret^
none caret  bang! "boom!"


EDIT

Here is a slightly modified example that better illustrates the various escape permutations that are required, depending on the context. The only case that requires unusual escaping is the last example when delayed expansion is on and there exists at least one ! on the line.

@echo off
setlocal DisableDelayedExpansion
echo DisableDelayedExpansion
echo caret^^       "caret^"
echo caret^^ bang! "caret^ bang!"

echo(
setlocal EnableDelayedExpansion
echo EnableDelayedExpansion
echo caret^^       "caret^"
echo caret^^^^ bang^^! "caret^^ bang^!"

-- OUTPUT --

DisableDelayedExpansion
caret^       "caret^"
caret^ bang! "caret^ bang!"

EnableDelayedExpansion
caret^       "caret^"
caret^ bang! "caret^ bang!"
2
  • I found this ^^! won't work when you want to print sequential !!! , that means ^^!^^!^^! will print nothing
    – Grace
    Commented Apr 10 at 14:39
  • @Grace Works for me as expected, tested with setlocal EnableDelayedExpansion & echo ^^!^^!^^!. Gives me three exclamation marks
    – jeb
    Commented Apr 10 at 18:19
19

To use an exclamation point in batch with Delayed Expansion enabled, you must first add the exclamation point to a variable with it disabled. See the below example with both DISABLEDELAYEDEXPANSION and ENABLEDELAYEDEXPANSION state.

@echo off
setlocal DISABLEDELAYEDEXPANSION
set DB_password=encrypt!Pws
echo %DB_password%
SETLOCAL ENABLEDELAYEDEXPANSION
echo !DB_password!
3
  • Best answer! This solves the use in scripts where DELAYEDEXPANSION is needed.
    – JasonXA
    Commented Jun 7, 2017 at 8:51
  • That's aberrative. Nowhere in the requirements is memory a concern and yet I use it a lot with no ill effect. Unless you can actually contribute, keep your nonsense to yourself.
    – JasonXA
    Commented Jan 10, 2018 at 4:36
  • I actually wrote my code like yours, then thought "this cannot work, because the ! will be lost with delayedExpansion enabled", searched the whole internet for an solution, landed here, read the last answer and found out, that my code would have worked all the time, I just didn't realized it only gets replaced once... Thanks a lot ^!^! ;D
    – timlg07
    Commented Oct 21, 2018 at 18:31
6

Thanks. To add to this valuable point, if one's script contains a variable whose value contains an "!", then the following approach will render that value as-is:

@echo off
SETLOCAL EnableDelayedExpansion
set /P omg=Enter a value that contains an exclamation-point:
echo Traditional: %omg%
echo Alternative: !omg!
pause

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.