What's the best way to check that environment variables are set in Unix shellscript

Posted by AndrewR on Stack Overflow See other posts from Stack Overflow or by AndrewR
Published on 2008-11-21T01:11:27Z Indexed on 2010/05/14 6:14 UTC
Read the original article Hit count: 354

Filed under:
|
|
|

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.

© Stack Overflow or respective owner

Related posts about unix

Related posts about shell