I saw this mentioned in some documentation. But it doesn't seem to do anything...
~ $ echo $DYNO
run.9917
~ $ echo ${DYNO}
run.9917
~ $ echo ${DYNO:-1}
run.9917
What does the :-1
do?
I saw this mentioned in some documentation. But it doesn't seem to do anything...
~ $ echo $DYNO
run.9917
~ $ echo ${DYNO}
run.9917
~ $ echo ${DYNO:-1}
run.9917
What does the :-1
do?
${DYNO:-1}
means if DYNO
exists and isn’t null, return its value; otherwise return 1
. It's the :-
between the variable's name and the default value inside the {}
that make this happen.
This is covered in the Shell Parameter Section of the Bash Reference Manual.
"${var:-1}"
means expand the parameter named var
if it's defined, and if not, expand 1
instead.
Other, similar expansions:
"${var: -1}"
means expand the substring of var
from the last character."${var:=1}"
means assign 1
to var
if it's not defined and then, either way, expand the parameter.Examples:
$ x=hi y=
$ echo "${x:-1}" "${x: -1}" "${y:-2}"
hi i 2
${var:-word}
If var
is null or unset, word
is substituted for var
. The value of var
does not change.
$> echo "${bar:-1}"
$> 1
$> bar=3
$> echo "${bar:-1}"
$> 3
$> echo "${bar-1}"
$> 3