I've seen cmd batch scripts using square notation to surround a variable. For example:
@echo off
if [%1]==[] (
echo no parameter entered
) else (
echo param1 is %1
)
What is the purpose of this?
I've seen cmd batch scripts using square notation to surround a variable. For example:
@echo off
if [%1]==[] (
echo no parameter entered
) else (
echo param1 is %1
)
What is the purpose of this?
The square brackets are needed to check for blanks because if you use:
Without the square brackets surrounding the variable, it will say
and exit.
it is used for proper syntax. Just imagine, you want to check, if a variable is empty:
obviously will fail. (wrong syntax)
Instead:
works fine.
Another "bad thing": you want to check a variable, but it may be empty:
works well, if %var% is not empty. But if it is empty, the line would be interpreted as:
obviously a syntax problem. But
would be interpreted as
correct syntax.
Instead of
"
you can use other chars. Some like[%var%]
, some use!
or.
Some people use only one char instead of surrounding the string likeif %var%.==.
The most common is surrounding with"
(because it will not fail if var contains an unquoted poison character like &.) *), but that depends on personal gust.*) Thanks to dbenham, this is a very important information