Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I escape an exclamation mark ! in cmd scripts?

Tags:

batch-file

cmd

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^! 
like image 616
Andy Morris Avatar asked Jul 20 '10 09:07

Andy Morris


People also ask

How do I get rid of the exclamation mark in CMD?

Jaime Ramos sent me this link where the solution can be found: use ^^! . The trick is that a single caret will be used to escape the exclamation mark in the first "pass" of command line interpretation, but delayed variable expansion adds a second "pass" where the exclamation mark will be interpreted.

How do you escape from CMD?

When working at the command line or with batch files, you must take one of two actions when you use strings that contain an ampersand. Either you must escape the ampersand by using the caret (^) symbol, or you must enclose the string inside quotation marks.

How do I escape exclamation mark in bash?

In addition to using single quotes for exclamations, in most shells you can also use a backslash \ to escape it.


2 Answers

That's what I found (^^)

@echo off setlocal ENABLEDELAYEDEXPANSION echo I want to go out with a bang^^! 
like image 88
FrVaBe Avatar answered Sep 22 '22 03:09

FrVaBe


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!" 
like image 37
jeb Avatar answered Sep 22 '22 03:09

jeb