mindthump icon

Bash Parameter Substitution and Expansion

mindthump | PRO | 08/29/21 05:51:47 PM UTC (Edited) | 0 ⭐ | 845 👁️ | Never ⏰ | []
Bash |

1 KB

|

None

|

0 👍

/

0 👎

Expression         Meaning
----------         --------
${var}             Value of var (same as $var)
${var-$DEFAULT}    If var not set, evaluate expression as $DEFAULT *
${var=$DEFAULT}    If var not set, evaluate expression as $DEFAULT *
${var:-$DEFAULT}   If var not set or is empty, evaluate expression as $DEFAULT *
${var:=$DEFAULT}   If var not set or is empty, evaluate expression as $DEFAULT *
${var+$OTHER}      If var set, evaluate expression as $OTHER, otherwise as null string
${var:+$OTHER}     If var set, evaluate expression as $OTHER, otherwise as null string
${var?$ERR_MSG}    If var not set, print $ERR_MSG and abort script with an exit status of 1.*
${var:?$ERR_MSG}   If var not set, print $ERR_MSG and abort script with an exit status of 1.*
${!varprefix*}     Matches all previously declared variables beginning with varprefix
${!varprefix@}     Matches all previously declared variables beginning with varprefix
 
* If var is set, evaluate the expression as $var with no side-effects.

Comments