<p>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:</p>

<pre><code>if [ -z "$STATE" ]; then
    echo "Need to set STATE"
    exit 1
fi  

if [ -z "$DEST" ]; then
    echo "Need to set DEST"
    exit 1
fi
</code></pre>

<p>which is a lot of typing. Is there a more elegant idiom for checking that a set of environment variables is set?</p>

<p>EDIT: I should mention that these variables have no meaningful default value - the script should error out if any are unset.</p>

```
```
${MyVariable:=SomeDefault}
```
```
If
```
MyVariable
```
is set and not null, it will reset the variable value (= nothing happens).
Else,
```
MyVariable
```
is set to
```
SomeDefault
```
.
The above will attempt to execute
```
${MyVariable}
```
, so if you just want to set the variable do:
```
```
MyVariable=${MyVariable:=SomeDefault}
```
```