What does the
-z flag mean in the code below:
if [ -z $TEST_PARAM ]; then
And is there a list of such flags?
For a flag like
ls -l, I know where to find it, but for a single flag, I didn't get any website describes it.
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. It only takes a minute to sign up.Sign up to join this community
[ is another name for the
test utility. The only difference between them is that
[ expects an extra
] parameter at the end. Most systems have a
[ man page with the same content as the
test man page.
Most shells have
test built in, so the exact capabilities described in the man page may differ slightly from those actually available in the shell.
-z is a standard feature available in all implementations though. Look for the description of the
test builtin or the section on conditional expressions to find operators like
[ -z $TEST_PARAM ] should actually be
[ -z "$TEST_PARAM" ], because variable expansions outside double quotes do far more than expand variables. See Why does my shell script choke on whitespace or other special characters? for more details. Without quotes, the value of
TEST_PARAM is split into words which are expanded as glob patterns. In contrast,
"$TEST_PARAM" expands to the value of the
TEST_PARAM variable. If the value of
TEST_PARAM is empty, then
$TEST_PARAM is expanded into a list of zero words, so the command that is executed is
[ -z ]. With only a single word to express the conditional expression, the conditional is true if this word is non-empty. Here
-z is not an operator but some non-empty string.
It means "if the parameter doesn't exist, do the following".
You can see this with:
$ ABC='1' $ $ if [ -z $ABC ]; then echo "ABC"; # Nothing fi; $ $ if [ -z $XYZ ]; then echo "yes"; # echo "Yes" fi; yes $
One example of where I use this, is in my
.bashrc file to invoke
tmux, but not recursively, as in:
[ -z $TMUX ] && export TERM=xterm-256color && exec tmux