0

What does it mean to have a variable inside square brackets? Boolean?

For example:

  1. [ $FILES ] || { print "File not found." }
  2. [ $VERIFY ] && { print "file verification fail" }
Benjamin W.
  • 46,058
  • 19
  • 106
  • 116
MR.Q
  • 3
  • 2
  • 3
    It is *testing* whether `FILES` and `VERIFY` are set. `[ ... ]` is equivalent to `test` in bash. If the variables are *unset*, then `[ $varname ]` will test false. – David C. Rankin Feb 01 '19 at 02:33

1 Answers1

0

[] invokes the test command which allows you to run conditional tests based on what arguments you supply inside them.

For the examples you have supplied:

  • $FILES is a single argument, so the test will return True if the argument is not null. Piped with || (Logical OR), whenever $FILES is 'null', test will return a non-zero exit status (1) thus printing "File not found."
  • Ditto for $VERIFY except being piped with && means that print "file verification" will execute as long as $VERIFY is not null (as test will return a status of 0 in this case).

I will also add that there also exists [[]] which is the 'newer' test and is more commonly used

EDIT: I would also recommend enclosing variable names in double quotes ("") as they are technically arguments to a command.

Mks
  • 104
  • 1
  • 5