fi
----
-== Optional parameters to commands
-
-If you want to call a command `cmd` with an option, if a variable is set, rather than doing:
-
-[,bash]
-----
-func() {
- local param="$1"
-
- if [[ $param ]]; then
- param="--this-special-option $param"
- fi
-
- cmd $param
-}
-----
-
-do it like this:
-
-[,bash]
-----
-func() {
- local param="$1"
-
- cmd ${param:+--this-special-option "$param"}
-}
-
-# cmd --this-special-option 'abc'
-func 'abc'
-
-# cmd
-func ''
-
-# cmd
-func
-----
-
-If you want to specify the option even with an empty string do this:
-
-[,bash]
-----
-func() {
- local -a special_params
-
- if [[ ${1+_} ]]; then
- # only declare `param` if $1 is set (even as null string)
- local param="$1"
- fi
-
- # check if `param` is set (even as null string)
- if [[ ${param+_} ]]; then
- special_params=( --this-special-option "${param}" )
- fi
-
- cmd ${param+"${special_params[@]}"}
-}
-
-# cmd --this-special-option 'abc'
-func 'abc'
-
-# cmd --this-special-option ''
-func ''
-
-# cmd
-func
-----
-
-Or more simple, if you only have to set an option:
-
-[,bash]
-----
-func() {
- if [[ ${1+_} ]]; then
- # only declare `param` if $1 is set (even as null string)
- local param="$1"
- fi
-
- cmd ${param+--this-special-option}
-}
-
-# cmd --this-special-option
-func 'abc'
-
-# cmd --this-special-option
-func ''
-
-# cmd
-func
-----