69 lines
2.6 KiB
Plaintext
69 lines
2.6 KiB
Plaintext
# Parameter Expansion
|
|
|
|
## Indirection
|
|
|
|
| Parameter | What does it do? |
|
|
| --------- | ---------------- |
|
|
| `${!VAR}` | Access a variable based on the value of `VAR`. See: [link](#assign-and-access-a-variable-using-a-variable)
|
|
| `${!VAR*}` | Expand to `IFS` separated list of variable names starting with `VAR`. |
|
|
| `${!VAR@}` | Expand to `IFS` separated list of variable names starting with `VAR`. |
|
|
|
|
|
|
## 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. <br> (`${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+` |
|
|
|
|
|
|
## Default Value
|
|
|
|
| Parameter | What does it do? |
|
|
| --------- | ---------------- |
|
|
| `${VAR:-STRING}` | If `VAR` is empty or unset, use `STRING` as it's value.
|
|
| `${VAR-STRING}` | If `VAR` is unset, use `STRING` as it's 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` isn't empty, use `STRING` as it's value.
|
|
| `${VAR+STRING}` | If `VAR` is set, use `STRING` as it's value.
|
|
| `${VAR:?STRING}` | Display an error if empty or unset.
|
|
| `${VAR?STRING}` | Display an error if unset.
|
|
|
|
|
|
<!-- CHAPTER END -->
|
|
|