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?
it is used for proper syntax. Just imagine, you want to check, if a variable is empty:
if %var%== echo bla
obviously will fail. (wrong syntax)
Instead:
if "%var%"=="" echo bla
works fine.
Another "bad thing": you want to check a variable, but it may be empty:
if %var%==bla echo bla
works well, if %var% is not empty. But if it is empty, the line would be interpreted as:
if ==bla echo bla
obviously a syntax problem. But
if "%var%"=="bla" echo bla
would be interpreted as
if ""=="bla" echo bla
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 like if %var%.==.
The most common is surrounding with "
(because it will not fail if var contains spaces or an unquoted poison character like &.) *), but that depends on personal gust.
*) Thanks to dbenham, this is a very important information
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With