pure-bash-bible/manuscript/chapter8.txt

71 lines
2.7 KiB
Plaintext
Raw Permalink Normal View History

2018-06-20 08:00:33 +02:00
# PARAMETER EXPANSION
2018-06-20 05:03:53 +02:00
## Indirection
2018-06-20 05:03:53 +02:00
| Parameter | What does it do? |
| --------- | ---------------- |
2018-06-20 08:05:56 +02:00
| `${!VAR}` | Access a variable based on the value of `VAR`.
2018-06-20 05:03:53 +02:00
| `${!VAR*}` | Expand to `IFS` separated list of variable names starting with `VAR`. |
2019-01-19 10:20:13 +01:00
| `${!VAR@}` | Expand to `IFS` separated list of variable names starting with `VAR`. If double-quoted, each variable name expands to a separate word. |
2018-06-20 05:03:53 +02:00
## Replacement
2018-06-20 05:03:53 +02:00
| 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.
2018-06-20 05:03:53 +02:00
## Length
2018-06-20 05:03:53 +02:00
| Parameter | What does it do? |
| --------- | ---------------- |
| `${#VAR}` | Length of var in characters.
| `${#ARR[@]}` | Length of array in elements.
2018-06-20 05:03:53 +02:00
## Expansion
2018-06-20 05:03:53 +02:00
| 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+` |
2018-06-20 05:03:53 +02:00
## 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+` |
2019-06-09 12:28:29 +02:00
| `${VAR~}` | Reverse case of first character. | `bash 4+` |
| `${VAR~~}` | Reverse case of all characters. | `bash 4+` |
2018-06-20 05:03:53 +02:00
## Default Value
| Parameter | What does it do? |
| --------- | ---------------- |
2018-06-20 14:08:15 +02:00
| `${VAR:-STRING}` | If `VAR` is empty or unset, use `STRING` as its value.
| `${VAR-STRING}` | If `VAR` is unset, use `STRING` as its value.
2018-06-20 05:03:53 +02:00
| `${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`.
2018-06-20 14:08:15 +02:00
| `${VAR:+STRING}` | If `VAR` is not empty, use `STRING` as its value.
| `${VAR+STRING}` | If `VAR` is set, use `STRING` as its value.
2018-06-20 05:03:53 +02:00
| `${VAR:?STRING}` | Display an error if empty or unset.
| `${VAR?STRING}` | Display an error if unset.
<!-- CHAPTER END -->