diff --git a/README.md b/README.md index f3c9e35..262e1f2 100644 --- a/README.md +++ b/README.md @@ -60,6 +60,12 @@ scripts and not full blown utilities. * [Check if string ends with sub-string.](#check-if-string-ends-with-sub-string) * [Variables](#variables) * [Assign and access a variable using a variable.](#assign-and-access-a-variable-using-a-variable) + * [Indirection](#indirection) + * [Replacement](#replacement) + * [Length](#length) + * [Expansion](#expansion) + * [Case Modification](#case-modification) + * [Default Value](#default-value) * [Arrays](#arrays) * [Reverse an array.](#reverse-an-array) * [Remove duplicate array elements.](#remove-duplicate-array-elements) @@ -489,6 +495,69 @@ var2="hello_${var1}" printf '%s\n' "${!var2}" ``` +## 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.
(`${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. + # Arrays