Question
I have a whole bunch of tests on variables in a bash (3.00) shell script where if the variable is not set, then it assigns a default, e.g.:
- if [ -z "${VARIABLE}" ]; then
- FOO='default'
- else
- FOO=${VARIABLE}
- fi
- FOO=${ ${VARIABLE} : 'default' }
Am I crazy, or does something like that exist?
How-To
There are two ways to carry out your request:
For example, let's do experiment with a simple shell script:
- test.sh
- #!/bin/sh
- echo "variable=${variable:-'defaultValue1'}"
- echo "variable=${variable}"
- echo "varialbe=${variable:='defaultValue2'}"
- echo "variable=${variable}"
Supplement
* [Linux 文章收集] 變數值代換 (variables substitution) 精簡筆記
沒有留言:
張貼留言