Shell Parameter Expansion
1 min readJan 31, 2022
Being a developer it is unlikely to have not played around with environment variables at one point or the other.
Considering that we will be spending more time with them in the foreseeable future, Shell parameter expansion is one great arrow to have in your quiver.
Let’s quickly learn the basics of it.
- To expand an env variable simply use:
echo $ENV_VARIABLE
- To check if the variable is present in the current shell or not use
echo ${ENV_VARIABLE-Not Present} //prints Not Present, as variable
not present
export ENV_VARIABLE= //variable present but not set
echo ${ENV_VARIABLE-Not Present} //prints nothing as variable is
present
- To check if the variable is unset, null or not present
echo ${ENV_VARIABLE:-Not Present} //prints Not Present, as
variable is not present
export ENV_VARIABLE= //variable present but not set
echo ${ENV_VARIABLE-Not Set} //prints Not Set as variable is
null
- Assign default value if the variable is unset, null or not present
echo ${ENV_VARIABLE:=DEFAULT_VALUE} //assigns and prints
DEFAULT_VALUE to variable as
variable not present
export ENV_VARIABLE=…