Miscellaneous improvements
This commit is contained in:
@@ -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 -->
|
||||
|
||||
Reference in New Issue
Block a user