Skip to content

Korn shell way of checking if environment variable is set

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.

Post a Comment

Your email is never published nor shared. Required fields are marked *
*
*