One way to do it is:
export API_BASE_URL=${API_BASE_URL:="http://localhost"}
If the variable already exists, use the current value. Otherwise, use http://localhost
. Only that the value is also set if the variable is an empty string. That is:
export API_BASE_URL="a"
export API_BASE_URL=${API_BASE_URL:="http://localhost"}
echo $API_BASE_URL # imprime "a"
export API_BASE_URL=""
export API_BASE_URL=${API_BASE_URL:="http://localhost"}
echo $API_BASE_URL # imprime "http://localhost"
export API_BASE_URL=${API_BASE_URL:="http://localhost"}
echo $API_BASE_URL # imprime "http://localhost"
Another option is to use -z
:
[ -z "$API_BASE_URL" ] && export API_BASE_URL="http://localhost"
With the same behavior as the first option (if the variable does not exist or is empty string, the new value is assigned).
But if you do not want to set the value if the variable already exists and is the empty string, switch to:
export API_BASE_URL=${API_BASE_URL="http://localhost"}
Tries: if [ -z "$var" ]; then echo "var is Blank"; Else echo "var is set to '$var'"; fi, where "var" you replace with its variable
– Paulo Martins