I've got a few Unix shell scripts where I need to check that certain environment variables are set before I start doing stuff, so I do this sort of thing:
if [ -z "$STATE" ]; then
echo "Need to set STATE"
exit 1
fi
if [ -z "$DEST" ]; then
echo "Need to set DEST"
exit 1
fi
which is a lot of typing. Is there a more elegant idiom for checking that a set of environment variables is set?
EDIT: I should mention that these variables have no meaningful default value - the script should error out if any are unset.
I always used:
Not that much more concise, I'm afraid.
Under CSH you have $?STATE.
Parameter Expansion
The obvious answer is to use one of the special forms of parameter expansion:
Or, better (see section on 'Position of double quotes' below):
The first variant (using just
?
) requires STATE to be set, but STATE="" (an empty string) is OK — not exactly what you want, but the alternative and older notation.The second variant (using
:?
) requires DEST to be set and non-empty.If you supply no message, the shell provides a default message.
The
${var?}
construct is portable back to Version 7 UNIX and the Bourne Shell (1978 or thereabouts). The${var:?}
construct is slightly more recent: I think it was in System III UNIX circa 1981, but it may have been in PWB UNIX before that. It is therefore in the Korn Shell, and in the POSIX shells, including specifically Bash.It is usually documented in the shell's man page in a section called Parameter Expansion. For example, the
bash
manual says:The Colon Command
I should probably add that the colon command simply has its arguments evaluated and then succeeds. It is the original shell comment notation (before '
#
' to end of line). For a long time, Bourne shell scripts had a colon as the first character. The C Shell would read a script and use the first character to determine whether it was for the C Shell (a '#
' hash) or the Bourne shell (a ':
' colon). Then the kernel got in on the act and added support for '#!/path/to/program
' and the Bourne shell got '#
' comments, and the colon convention went by the wayside. But if you come across a script that starts with a colon, now you will know why.Position of double quotes
blong asked in a comment:
The gist of the discussion is:
The short answer is "do as
shellcheck
suggests":To illustrate why, study the following. Note that the
:
command doesn't echo its arguments (but the shell does evaluate the arguments). We want to see the arguments, so the code below usesprintf "%s\n"
in place of:
.Note how the value in
$x
is expanded to first*
and then a list of file names when the overall expression is not in double quotes. This is whatshellcheck
is recommending should be fixed. I have not verified that it doesn't object to the form where the expression is enclosed in double quotes, but it is a reasonable assumption that it would be OK.Your question is dependent on the shell that you are using.
Bourne shell leaves very little in the way of what you're after.
BUT...
It does work, just about everywhere.
Just try and stay away from csh. It was good for the bells and whistles it added, compared the Bourne shell, but it is really creaking now. If you don't believe me, just try and separate out STDERR in csh! (-:
There are two possibilities here. The example above, namely using:
for the first time you need to refer to $MyVariable. This takes the env. var MyVariable and, if it is currently not set, assigns the value of SomeDefault to the variable for later use.
You also have the possibility of:
which just substitutes SomeDefault for the variable where you are using this construct. It doesn't assign the value SomeDefault to the variable, and the value of MyVariable will still be null after this statement is encountered.
Rather than using external shell scripts I tend to load in functions in my login shell. I use something like this as a helper function to check for environment variables rather than any set variable:
In my opinion the simplest and most compatible check for #!/bin/sh is:
Again, this is for /bin/sh and is compatible also on old Solaris systems.
We can write a nice assertion to check a bunch of variables all at once:
Sample invocation:
Output: