I'm looking for a way to set an exported environment variable in a Bash script to a default value specified literally in a script only if the environment variable was previously not set to any value (even an empty string), and to do this without mentioning the name of the environment variable more than once in the code that solves this task.
Note that the solutions here and here (using ${FOO:=42}
) do not apply because they only set a shell variable, not an exported environment variable. I don't want to set it for a single command; I want to set it for all commands that follow from the same shell.
Also note that something like
export FOO="${FOO:-42}"
does not answer this question either as it mentions FOO
twice in the solution.
I realize this question has an easy technical alternative solution — just use the solution that mentions the variable name twice. To be explicit, "no, that cannot be done" is a reasonable answer to this question, if it is indeed not possible, and those alternatives are perfectly fine if it's not a problem to mention the variable twice, so it's a slightly artificial problem (I only care about this to make editing the script slightly more clean and easy).