diff --git a/README.md b/README.md index 43555ca..56f174b 100644 --- a/README.md +++ b/README.md @@ -274,7 +274,7 @@ $ regex "red" '^(#?([a-fA-F0-9]{6}|[a-fA-F0-9]{3}))$' ```shell is_hex_color() { - if [[ "$1" =~ ^(#?([a-fA-F0-9]{6}|[a-fA-F0-9]{3}))$ ]]; then + if [[ $1 =~ ^(#?([a-fA-F0-9]{6}|[a-fA-F0-9]{3}))$ ]]; then printf '%s\n' "${BASH_REMATCH[1]}" else printf '%s\n' "error: $1 is an invalid color." @@ -487,17 +487,17 @@ The Quick Brown **Using a test:** ```shell -if [[ "$var" == *sub_string* ]]; then +if [[ $var == *sub_string* ]]; then printf '%s\n' "sub_string is in var." fi # Inverse (substring not in string). -if [[ "$var" != *sub_string* ]]; then +if [[ $var != *sub_string* ]]; then printf '%s\n' "sub_string is not in var." fi # This works for arrays too! -if [[ "${arr[*]}" == *sub_string* ]]; then +if [[ ${arr[*]} == *sub_string* ]]; then printf '%s\n' "sub_string is in array." fi ``` @@ -523,12 +523,12 @@ esac ## Check if string starts with sub-string ```shell -if [[ "$var" == sub_string* ]]; then +if [[ $var == sub_string* ]]; then printf '%s\n' "var starts with sub_string." fi # Inverse (var does not start with sub_string). -if [[ "$var" != sub_string* ]]; then +if [[ $var != sub_string* ]]; then printf '%s\n' "var does not start with sub_string." fi ``` @@ -536,12 +536,12 @@ fi ## Check if string ends with sub-string ```shell -if [[ "$var" == *sub_string ]]; then +if [[ $var == *sub_string ]]; then printf '%s\n' "var ends with sub_string." fi # Inverse (var does not end with sub_string). -if [[ "$var" != *sub_string ]]; then +if [[ $var != *sub_string ]]; then printf '%s\n' "var does not end with sub_string." fi ``` @@ -600,7 +600,7 @@ remove_array_dups() { declare -A tmp_array for i in "$@"; do - [[ "$i" ]] && IFS=" " tmp_array["${i:- }"]=1 + [[ $i ]] && IFS=" " tmp_array["${i:- }"]=1 done printf '%s\n' "${!tmp_array[@]}" @@ -723,12 +723,12 @@ arr=(apples oranges tomatoes) # Elements and index. for i in "${!arr[@]}"; do - printf '%s\n' "${arr[$i]}" + printf '%s\n' "${arr[i]}" done # Alternative method. for ((i=0;i<${#arr[@]};i++)); do - printf '%s\n' "${arr[$i]}" + printf '%s\n' "${arr[i]}" done ``` @@ -945,11 +945,11 @@ printf '' >file extract() { # Usage: extract file "opening marker" "closing marker" while IFS=$'\n' read -r line; do - [[ "$extract" && "$line" != "$3" ]] && \ + [[ $extract && $line != "$3" ]] && printf '%s\n' "$line" - [[ "$line" == "$2" ]] && extract=1 - [[ "$line" == "$3" ]] && extract= + [[ $line == "$2" ]] && extract=1 + [[ $line == "$3" ]] && extract= done < "$1" } ``` @@ -1022,14 +1022,28 @@ Downloads ## Assign and access a variable using a variable ```shell -hello_world="test" +$ hello_world="value" # Create the variable name. -var1="world" -var2="hello_${var1}" +$ var="world" +$ ref="hello_$var" -# Print the value of the variable name stored in 'hello_$var1'. -printf '%s\n' "${!var2}" +# Print the value of the variable name stored in 'hello_$var'. +$ printf '%s\n' "${!ref}" +value +``` + +Alternatively, on `bash` 4.3+: + +```shell +$ hello_world="value" +$ var="world" + +# Declare a nameref. +$ declare -n ref=hello_$var + +$ printf '%s\n' "$ref" +value ``` @@ -1101,7 +1115,7 @@ Contrary to popular belief, there is no issue in utilizing raw escape sequences. | --------- | ---------------- | | `${!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`. | +| `${!VAR@}` | Expand to `IFS` separated list of variable names starting with `VAR`. If double-quoted, each variable name expands to a separate word. | ## Replacement @@ -1741,16 +1755,16 @@ f()for i in "$@"; do echo "$i"; done ```shell # One line # Note: The 3rd statement may run when the 1st is true -[[ "$var" == hello ]] && echo hi || echo bye -[[ "$var" == hello ]] && { echo hi; echo there; } || echo bye +[[ $var == hello ]] && echo hi || echo bye +[[ $var == hello ]] && { echo hi; echo there; } || echo bye # Multi line (no else, single statement) # Note: The exit status may not be the same as with an if statement -[[ "$var" == hello ]] && \ +[[ $var == hello ]] && echo hi # Multi line (no else) -[[ "$var" == hello ]] && { +[[ $var == hello ]] && { echo hi # ... } @@ -1882,7 +1896,7 @@ Fri 15 Jun - 10:00 AM ```shell $ : \\u -# Expand the parameter as if it were a prompt string +# Expand the parameter as if it were a prompt string. $ printf '%s\n' "${_@P}" black ``` diff --git a/manuscript/chapter1.txt b/manuscript/chapter1.txt index 42f2d40..1dd4b05 100644 --- a/manuscript/chapter1.txt +++ b/manuscript/chapter1.txt @@ -100,7 +100,7 @@ $ regex "red" '^(#?([a-fA-F0-9]{6}|[a-fA-F0-9]{3}))$' ```shell is_hex_color() { - if [[ "$1" =~ ^(#?([a-fA-F0-9]{6}|[a-fA-F0-9]{3}))$ ]]; then + if [[ $1 =~ ^(#?([a-fA-F0-9]{6}|[a-fA-F0-9]{3}))$ ]]; then printf '%s\n' "${BASH_REMATCH[1]}" else printf '%s\n' "error: $1 is an invalid color." @@ -313,17 +313,17 @@ The Quick Brown **Using a test:** ```shell -if [[ "$var" == *sub_string* ]]; then +if [[ $var == *sub_string* ]]; then printf '%s\n' "sub_string is in var." fi # Inverse (substring not in string). -if [[ "$var" != *sub_string* ]]; then +if [[ $var != *sub_string* ]]; then printf '%s\n' "sub_string is not in var." fi # This works for arrays too! -if [[ "${arr[*]}" == *sub_string* ]]; then +if [[ ${arr[*]} == *sub_string* ]]; then printf '%s\n' "sub_string is in array." fi ``` @@ -349,12 +349,12 @@ esac ## Check if string starts with sub-string ```shell -if [[ "$var" == sub_string* ]]; then +if [[ $var == sub_string* ]]; then printf '%s\n' "var starts with sub_string." fi # Inverse (var does not start with sub_string). -if [[ "$var" != sub_string* ]]; then +if [[ $var != sub_string* ]]; then printf '%s\n' "var does not start with sub_string." fi ``` @@ -362,12 +362,12 @@ fi ## Check if string ends with sub-string ```shell -if [[ "$var" == *sub_string ]]; then +if [[ $var == *sub_string ]]; then printf '%s\n' "var ends with sub_string." fi # Inverse (var does not end with sub_string). -if [[ "$var" != *sub_string ]]; then +if [[ $var != *sub_string ]]; then printf '%s\n' "var does not end with sub_string." fi ``` diff --git a/manuscript/chapter18.txt b/manuscript/chapter18.txt index a683fae..b519588 100644 --- a/manuscript/chapter18.txt +++ b/manuscript/chapter18.txt @@ -95,16 +95,16 @@ f()for i in "$@"; do echo "$i"; done ```shell # One line # Note: The 3rd statement may run when the 1st is true -[[ "$var" == hello ]] && echo hi || echo bye -[[ "$var" == hello ]] && { echo hi; echo there; } || echo bye +[[ $var == hello ]] && echo hi || echo bye +[[ $var == hello ]] && { echo hi; echo there; } || echo bye # Multi line (no else, single statement) # Note: The exit status may not be the same as with an if statement -[[ "$var" == hello ]] && \ +[[ $var == hello ]] && echo hi # Multi line (no else) -[[ "$var" == hello ]] && { +[[ $var == hello ]] && { echo hi # ... } diff --git a/manuscript/chapter19.txt b/manuscript/chapter19.txt index bf9ec70..168b3c8 100644 --- a/manuscript/chapter19.txt +++ b/manuscript/chapter19.txt @@ -88,7 +88,7 @@ Fri 15 Jun - 10:00 AM ```shell $ : \\u -# Expand the parameter as if it were a prompt string +# Expand the parameter as if it were a prompt string. $ printf '%s\n' "${_@P}" black ``` diff --git a/manuscript/chapter2.txt b/manuscript/chapter2.txt index 2d7c27c..08c3b22 100644 --- a/manuscript/chapter2.txt +++ b/manuscript/chapter2.txt @@ -49,7 +49,7 @@ remove_array_dups() { declare -A tmp_array for i in "$@"; do - [[ "$i" ]] && IFS=" " tmp_array["${i:- }"]=1 + [[ $i ]] && IFS=" " tmp_array["${i:- }"]=1 done printf '%s\n' "${!tmp_array[@]}" diff --git a/manuscript/chapter3.txt b/manuscript/chapter3.txt index 75f88de..34e8edb 100644 --- a/manuscript/chapter3.txt +++ b/manuscript/chapter3.txt @@ -41,12 +41,12 @@ arr=(apples oranges tomatoes) # Elements and index. for i in "${!arr[@]}"; do - printf '%s\n' "${arr[$i]}" + printf '%s\n' "${arr[i]}" done # Alternative method. for ((i=0;i<${#arr[@]};i++)); do - printf '%s\n' "${arr[$i]}" + printf '%s\n' "${arr[i]}" done ``` diff --git a/manuscript/chapter4.txt b/manuscript/chapter4.txt index 2cb872c..e7bb50a 100644 --- a/manuscript/chapter4.txt +++ b/manuscript/chapter4.txt @@ -167,11 +167,11 @@ printf '' >file extract() { # Usage: extract file "opening marker" "closing marker" while IFS=$'\n' read -r line; do - [[ "$extract" && "$line" != "$3" ]] && \ + [[ $extract && $line != "$3" ]] && printf '%s\n' "$line" - [[ "$line" == "$2" ]] && extract=1 - [[ "$line" == "$3" ]] && extract= + [[ $line == "$2" ]] && extract=1 + [[ $line == "$3" ]] && extract= done < "$1" } ``` diff --git a/manuscript/chapter6.txt b/manuscript/chapter6.txt index 8ff5947..5a280d1 100644 --- a/manuscript/chapter6.txt +++ b/manuscript/chapter6.txt @@ -3,14 +3,28 @@ ## Assign and access a variable using a variable ```shell -hello_world="test" +$ hello_world="value" # Create the variable name. -var1="world" -var2="hello_${var1}" +$ var="world" +$ ref="hello_$var" -# Print the value of the variable name stored in 'hello_$var1'. -printf '%s\n' "${!var2}" +# Print the value of the variable name stored in 'hello_$var'. +$ printf '%s\n' "${!ref}" +value +``` + +Alternatively, on `bash` 4.3+: + +```shell +$ hello_world="value" +$ var="world" + +# Declare a nameref. +$ declare -n ref=hello_$var + +$ printf '%s\n' "$ref" +value ``` diff --git a/manuscript/chapter8.txt b/manuscript/chapter8.txt index acd7f21..a4f4efb 100644 --- a/manuscript/chapter8.txt +++ b/manuscript/chapter8.txt @@ -6,7 +6,7 @@ | --------- | ---------------- | | `${!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`. | +| `${!VAR@}` | Expand to `IFS` separated list of variable names starting with `VAR`. If double-quoted, each variable name expands to a separate word. | ## Replacement