Keyboard shortcuts

Press or to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Shell Parameter Expansion

https://www.gnu.org/software/bash/manual/html_node/Shell-Parameter-Expansion.html

$

If parameter is unset or null, the expansion of word is substituted.\ Otherwise, the value of parameter is substituted.

$

If parameter is unset or null, the expansion of word is assigned to parameter. The value of parameter is then substituted. Positional parameters and special parameters may not be assigned to in this way.

$

If parameter is null or unset, the expansion of word (or a message to that effect if word is not present) is written to the standard error and the shell, if it is not interactive, exits. Otherwise, the value of parameter is substituted.

$

If parameter is null or unset, nothing is substituted, otherwise the expansion of word is substituted.

${parameter:offset} and $

$ a="12345"
$ echo ${a:1:2}
23
$ echo ${a:1:-1}
234

${parameter#word} and $

  • # deletes the shortest match.
  • ## deletes the longest match.
$ a="1231245"
$ echo ${a#*1}
231245
$ echo ${a##*1}
245

${parameter%word} and $

Same as above but matches from end (trailling).

$

Substitutes first pattern match with string.

${parameter^pattern} and ${parameter^^pattern} and ${parameter,pattern} and $

  • ^ makes upper-case.
  • , makes lower-case.

Makes all matched characters in pattern upper/lower-case. Pattern should now atempt to match more than 1 character.