Miscellaneous improvements

This commit is contained in:
Crestwave
2019-01-19 17:20:13 +08:00
parent 2b558f2962
commit 2414ae9f42
9 changed files with 78 additions and 50 deletions

View File

@@ -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
```
<!-- CHAPTER END -->