0

What is is meaning of -a -z in

if [ -z "$ENV_VAR" -a -z "$ENV_VAR2"]; then
...
fi

bash conditional?

The first -z checks if $ENV_VAR defined according to

-z string True if the length of string is zero.

What does -a -z combination test with relation to ENV_VAR2?

according to the docs

-a file True if file exists.

however, ENV_VAR2 may contain text only, not a file name.

rok
  • 7,346
  • 16
  • 58
  • 104

4 Answers4

2

[ -z "$ENV_VAR" -a -z "$ENV_VAR2" ] has 2 conditions ANDed together using -a switch:

What it means is this:

  • -z "$ENV_VAR": $ENV_VAR is empty
  • -a: and
  • -z "$ENV_VAR2": $ENV_VAR2 is empty

btw if you're using bash you can refactor this condition to make it bit more succinct:

[[ -z $ENV_VAR && -z $ENV_VAR2 ]]
anubhava
  • 713,503
  • 59
  • 514
  • 593
  • 2
    If you're not using `[[ ]]`, you should use `[ ... ] && [ ... ]` instead, see [Bash Pitfall #6](https://mywiki.wooledge.org/BashPitfalls#pf6). – Benjamin W. Jan 07 '19 at 17:02
1

Please try this "man test".

Ideally, in that output, you'll see that -a performs an "AND" between two expressions.

Mark
  • 3,571
  • 1
  • 17
  • 23
  • That gets you the man page for the external `test`/`[` command, but in Bash, they're built-ins, the help for which is in `help test` (or `man bash`). – Benjamin W. Jan 07 '19 at 17:05
1

It's "and".

See man test

EXPRESSION1 -a EXPRESSION2
  both EXPRESSION1 and EXPRESSION2 are true

Examples:

$ [ -z "" -a -z "" ] && echo Hello
Hello

$ [[ -z "" -a -z "" ]] && echo Hello
bash: syntax error in conditional expression
bash: syntax error near `-a'

If used with single [ it is the "and" from test. If used with [[ it is the file check from bash.

The bash solution:

$ [[ -z "" && -z "" ]] && echo Hello
Hello
Ralf
  • 1,653
  • 6
  • 14
1

For POSIX compatibility, [[ ... && ... ]] is not available, but -a is considered obsolete (and optional) by POSIX, so use two separate [ commands instead.

if [ -z "$ENV_VAR" ] && [ -z "$ENV_VAR2" ]; then
...
fi
chepner
  • 446,329
  • 63
  • 468
  • 610