Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

BASH: "local var=${3-16}" meaning not clear

Tags:

linux

bash

shell

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

like image 265
Mindaugas Bernatavičius Avatar asked Dec 01 '22 00:12

Mindaugas Bernatavičius


2 Answers

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.

like image 185
fredtantini Avatar answered Dec 04 '22 06:12

fredtantini


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".

like image 40
rici Avatar answered Dec 04 '22 05:12

rici