user5096780
user5096780

Reputation:

How to echo a closing parenthesis literally in an if/else block?

So here is my code:

if "!%currentuser%program1!" == "None" (
  REM EASTER EGG this is here because i am not confident with using the not switch
) 
ELSE 
(
  echo 1)!%currentuser%program1!
)

And it crashes because where it says:

echo 1)!%currentuser%program1!

The closing parenthesis is used as an end to the if statement but I want it to be literally echoed.
I think the interpreter sees this:

if "!%currentuser%program1!" == "None" 
(
  REM EASTER EGG this is here because i am not confident with using the not switch
) 
ELSE
(
  echo 1
)!%currentuser%program1!
)

And the last 2 lines are obviously an error.
How can I make it actually echo the closing parenthesis?

Upvotes: 1

Views: 65

Answers (2)

Magoo
Magoo

Reputation: 80033

  1. There must be a separator before the opening parenthesis of the IF true-condition target
  2. That opening parenthesis must be on the same physical line as the if
  3. Where an 'else' clause is used, the ending parenthesis of the "true" block, a separator and the else keyword must be on the same physical line
  4. Where an 'else' block is used, the else keyword, a separator and the opening parenthesis of the "else" block must be on the same physical line

ie

if condition (
 something
) else (
 someotherthing
)

Also, you must escape the literal ) otherwise batch interprets it as an end-of-block

Upvotes: 1

benPearce
benPearce

Reputation: 38333

Try adding a caret (^) symbol prefix to the bracket you want to echo.

http://www.robvanderwoude.com/escapechars.php

Upvotes: 2

Related Questions