Trying to understand some BASH script I encountered this line
local var=${3-16}
I understand the assignment part and the local part - my question is what does the dash indicate in "${3-16}".
If I try:
$ maxi=${1-45}; echo $maxi
45 <-- result
Please explain the meaning of the dash. Thanks
When doing ${parameter-default}
(or ${parameter:-default}
), if parameter
is not set, it will use the default
value.
So in var=${3-16}
, if $3
is not set, var
will be 16, otherwise, var
will be $3
.
You can check Advanced Bash-Scripting Guide for more examples, and other substitutions.
It means "unless the parameter is unassigned, in which case use...". (:-
would mean "unless the parameter is empty or unassigned".) So ${3-16}
means "$3
if it exists, otherwise 16
".
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With