This may have been answered in other SO posts but I don't think I can find exactly what I am looking for.
I have a (#!/usr/bin/env bash) function that checks for the existence of an (environment) variable
checkForVariable() {
if [[ -z $1 ]]; then
echo "Error: Define $1 environment variable"
exit 1
fi
}
but in the error message I want it to tell me which env variable is missing.
If I call it using
checkForVariable "${ENVIRONMENT_NAME}"
and if ENVIRONMENT_NAME is not set then obviously I will get Error: Define environment variable which is not useful.
How can I change my function so that I can pass checkForVariable a string and not a variable reference i.e.
checkForVariable "ENVIRONMENT_NAME"
I've tried eg. if [[ -z "\$${1}" ]]; then and if [[ -z "${$1}" ]]; then but these don't work.