# PARAMETER EXPANSION ## Indirection | Parameter | What does it do? | | --------- | ---------------- | | `${!VAR}` | Access a variable based on the value of `VAR`. | `${!VAR*}` | Expand to `IFS` separated list of variable names starting with `VAR`. | | `${!VAR@}` | Expand to `IFS` separated list of variable names starting with `VAR`. If double-quoted, each variable name expands to a separate word. | ## Replacement | Parameter | What does it do? | | --------- | ---------------- | | `${VAR#PATTERN}` | Remove shortest match of pattern from start of string. | | `${VAR##PATTERN}` | Remove longest match of pattern from start of string. | | `${VAR%PATTERN}` | Remove shortest match of pattern from end of string. | | `${VAR%%PATTERN}` | Remove longest match of pattern from end of string. | | `${VAR/PATTERN/REPLACE}` | Replace first match with string. | `${VAR//PATTERN/REPLACE}` | Replace all matches with string. | `${VAR/PATTERN}` | Remove first match. | `${VAR//PATTERN}` | Remove all matches. ## Length | Parameter | What does it do? | | --------- | ---------------- | | `${#VAR}` | Length of var in characters. | `${#ARR[@]}` | Length of array in elements. ## Expansion | Parameter | What does it do? | | --------- | ---------------- | | `${VAR:OFFSET}` | Remove first `N` chars from variable. | `${VAR:OFFSET:LENGTH}` | Get substring from `N` character to `N` character.
(`${VAR:10:10}`: Get sub-string from char `10` to char `20`) | `${VAR:: OFFSET}` | Get first `N` chars from variable. | `${VAR:: -OFFSET}` | Remove last `N` chars from variable. | `${VAR: -OFFSET}` | Get last `N` chars from variable. | `${VAR:OFFSET:-OFFSET}` | Cut first `N` chars and last `N` chars. | `bash 4.2+` | ## Case Modification | Parameter | What does it do? | CAVEAT | | --------- | ---------------- | ------ | | `${VAR^}` | Uppercase first character. | `bash 4+` | | `${VAR^^}` | Uppercase all characters. | `bash 4+` | | `${VAR,}` | Lowercase first character. | `bash 4+` | | `${VAR,,}` | Lowercase all characters. | `bash 4+` | | `${VAR~}` | Reverse case of first character. | `bash 4+` | | `${VAR~~}` | Reverse case of all characters. | `bash 4+` | ## Default Value | Parameter | What does it do? | | --------- | ---------------- | | `${VAR:-STRING}` | If `VAR` is empty or unset, use `STRING` as its value. | `${VAR-STRING}` | If `VAR` is unset, use `STRING` as its value. | `${VAR:=STRING}` | If `VAR` is empty or unset, set the value of `VAR` to `STRING`. | `${VAR=STRING}` | If `VAR` is unset, set the value of `VAR` to `STRING`. | `${VAR:+STRING}` | If `VAR` is not empty, use `STRING` as its value. | `${VAR+STRING}` | If `VAR` is set, use `STRING` as its value. | `${VAR:?STRING}` | Display an error if empty or unset. | `${VAR?STRING}` | Display an error if unset.