Home > Back-end >  Significance of curly braces in string comparison (.bat)
Significance of curly braces in string comparison (.bat)

Time:08-25

So I have to modify a batch script which I notice has this style of parsing command-line flags, for example:

if /I {%1} == {-d} (

Is there any reason why curly braces are used? As far as I know, they don't really have any significance in batch scripts. Interestingly, the commit history shows that curly braces were introduced after code review, and the pre-review version used square brackets:

if /I [%1] == [-d] (

Initial coders/reviewers are gone, so I'm asking here why these braces/brackets would be necessary, and whether it's any different than using doublequotes instead, or even no quotes at all.

CodePudding user response:

It's one of many methods used to protect against %1 being unspecified, which leads to its being replaced by nothing and hence a syntax error as if == whatever ( is illegal. Braces were used, brackets, simply pre/succeeding with a character... Normally, "quoting both sides" of the comparison operator is used.

Or

set "somevar=%~1"
if defined somevar ...
  • Related