| 1 | #!/bin/bash |
| 2 | |
| 3 | # Indirect variable referencing. |
| 4 | # This has a few of the attributes of references in C++. |
| 5 | |
| 6 | |
| 7 | a=letter_of_alphabet |
| 8 | letter_of_alphabet=z |
| 9 | |
| 10 | echo "a = $a" # Direct reference. |
| 11 | |
| 12 | echo "Now a = ${!a}" # Indirect reference. |
| 13 | # The ${!variable} notation is more intuitive than the old |
| 14 | #+ eval var1=\$$var2 |
| 15 | |
| 16 | echo |
| 17 | |
| 18 | t=table_cell_3 |
| 19 | table_cell_3=24 |
| 20 | echo "t = ${!t}" # t = 24 |
| 21 | table_cell_3=387 |
| 22 | echo "Value of t changed to ${!t}" # 387 |
| 23 | # No 'eval' necessary. |
| 24 | |
| 25 | # This is useful for referencing members of an array or table, |
| 26 | #+ or for simulating a multi-dimensional array. |
| 27 | # An indexing option (analogous to pointer arithmetic) |
| 28 | #+ would have been nice. Sigh. |
| 29 | |
| 30 | exit 0 |
| 31 | |
| 32 | # See also, ind-ref.sh example. |