I am writing a shell script. In this shell script, I am have a variable that either takes a default value, or the value of an environment variable. However, the environment variable doesn't have to be present.
For instance, assume, before running the script, I perform the following operation:
export DEPLOY_ENV=dev
How do I tell the script to search for this environment variable, and store its value in a variable inside the script. Moreover, how do I tell the script that if this environment variable does not exist, store a default variable?
printenv DEPLOY_ENV
should do itLANG
into the shell variabletempV
. As for the other suggestions in this thread: Note that with none of them, you can distinguish, whether you have an environment variable or a shell variable. If this distinction is really important, the solution suggested by @sjsam should be considered.: ${MY_VARIABLE:?}
It will print a message and return with non-0 code if it doesn't exist:-bash: MY_VARIABLE: parameter null or not set
. See also null command