Tech and travel

Korn shell way of checking if environment variable is set

2007-07-04

Sometimes you have to check if a variable is set and exit if it’s not. The following code does it in a nice and simple way :

error_mess="This environment variable should be set"

: ${ORACLE_HOME:?$error_mess}

The output looks like this :

./script.sh[3]: ORACLE_HOME: This environment variable should be set

The script exits after checking ORACLE_HOME, none of the following commands get executed.

I found this in the sample chapter of the The Korn Shell: Unix & Linux Programming Manual, 3rd Edition book.

Copyright (c) 2024 Michel Hollands