A construção ${var:-val}
usará um valor padrão de val
se a variável var
não estiver definida no momento dessa invocação. É apenas uma das muitas expansões de parâmetros úteis. Eu tenho um script que irá gerar essa folha de dicas útil:
${V} Base string |reallyextremelylongfilename.ext
--- Default substitutions ---
${nullvar} Provided example case |
${#nullvar-def} Default value if unset or null |def
${#nullvar:-def} Default value if unset |def
--- Default assignments ---
${1} Provided example case |
${$1=def} Default value if unset or null |def
${$1:=def} Default value if unset |def
--- String metadata ---
${#V} String length |31
--- Substring extraction ---
${V:6} Substring from position |extremelylongfilename.ext
${V:6:9} Substring with length from pos. |extremely
--- Substring deletion ---
${V#*a} Delete shortest prefix match |llyextremelylongfilename.ext
${V##*a} Delete longest prefix match |me.ext
${V%e*} Delete shortest suffix match |reallyextremelylongfilename.
${V%%e*} Delete longest suffix match |r
--- Substring replacement ---
${V/long/short} Replace first match |reallyextremelyshortfilename.ext
${V/#r*a/REA} Replace prefix match |REAme.ext
${V/%.e*/.dat} Replace suffix match |reallyextremelylongfilename.dat
${V//e/II} Replace all matches |rIIallyIIxtrIImIIlylongfilIInamII.IIxt
--- Other handy things ---
${V?Message} exit 1 with 'Message' output if V is not set or is null
${V:?Message} exit 1 with 'Message' output if V is not set
${V+Value} If V is set, use 'Value', otherwise null