bash - Insert variable value or default value if empty in a string -


i want insert value of environment variable in string or default value if corresponding variable not initialized.

example:

if [ -z $my_var ];     my_var="default" fi  echo "my variable contains $my_var" 

i'm using lot of variables in strings , tests cluttering script.

is there way make ternary expression in string?

example of want achieve (it doesn't work):

echo "my variable contains ${-z $my_var ? $my_var : 'default'}" 

see bash default values

→ echo "my variable contains ${my_var:-default}" variable contains default 

Comments