| 1 | #!/bin/bashscip-shell filesystem . . `/bin/bash`. |
| 2 | # ind-ref.sh: Indirect variable referencing. |
| 3 | # Accessing the contents of the contents of a variable. |
| 4 | |
| 5 | # First, let's fool around a little. |
| 6 | |
| 7 | var=23 |
| 8 | |
| 9 | echoscip-shell system . . echo. "\$var = $var" # $var = 23 |
| 10 | # So far, everything as expected. But ... |
| 11 | |
| 12 | echoscip-shell system . . echo. "\$\$var = $$var" # $$var = 4570var |
| 13 | # Not useful ... |
| 14 | # \$\$ expanded to PID of the script |
| 15 | # -- refer to the entry on the $$ variable -- |
| 16 | #+ and "var" is echoed as plain text. |
| 17 | # (Thank you, Jakob Bohm, for pointing this out.) |
| 18 | |
| 19 | echoscip-shell system . . echo. "\\\$\$var = \$$var" # \$$var = $23 |
| 20 | # As expected. The first $ is escaped and pasted on to |
| 21 | #+ the value of var ($var = 23 ). |
| 22 | # Meaningful, but still not useful. |
| 23 | |
| 24 | # Now, let's start over and do it the right way. |
| 25 | |
| 26 | # ============================================== # |
| 27 | |
| 28 | |
| 29 | a=letter_of_alphabet # Variable "a" holds the name of another variable. |
| 30 | letter_of_alphabet=z |
| 31 | |
| 32 | echoscip-shell system . . echo. |
| 33 | |
| 34 | # Direct reference. |
| 35 | echoscip-shell system . . echo. "a = $a" # a = letter_of_alphabet |
| 36 | |
| 37 | # Indirect reference. |
| 38 | eval a=\$$a |
| 39 | # ^^^ Forcing an eval(uation), and ... |
| 40 | # ^ Escaping the first $ ... |
| 41 | # ------------------------------------------------------------------------ |
| 42 | # The 'eval' forces an update of $a, sets it to the updated value of \$$a. |
| 43 | # So, we see why 'eval' so often shows up in indirect reference notation. |
| 44 | # ------------------------------------------------------------------------ |
| 45 | echoscip-shell system . . echo. "Now a = $a" # Now a = z |
| 46 | |
| 47 | echoscip-shell system . . echo. |
| 48 | |
| 49 | |
| 50 | # Now, let's try changing the second-order reference. |
| 51 | |
| 52 | t=table_cell_3 |
| 53 | table_cell_3=24 |
| 54 | echoscip-shell system . . echo. "\"table_cell_3\" = $table_cell_3" # "table_cell_3" = 24 |
| 55 | echoscip-shell system . . echo. -n "dereferenced \"t\" = "; eval echo \$$t # dereferenced "t" = 24 |
| 56 | # In this simple case, the following also works (why?). |
| 57 | # eval t=\$$t; echo "\"t\" = $t" |
| 58 | |
| 59 | echoscip-shell system . . echo. |
| 60 | |
| 61 | t=table_cell_3 |
| 62 | NEW_VAL=387 |
| 63 | table_cell_3=$NEW_VAL |
| 64 | echoscip-shell system . . echo. "Changing value of \"table_cell_3\" to $NEW_VAL." |
| 65 | echoscip-shell system . . echo. "\"table_cell_3\" now $table_cell_3" |
| 66 | echoscip-shell system . . echo. -n "dereferenced \"t\" now "; eval echo \$$t |
| 67 | # "eval" takes the two arguments "echo" and "\$$t" (set equal to $table_cell_3) |
| 68 | |
| 69 | |
| 70 | echoscip-shell system . . echo. |
| 71 | |
| 72 | # (Thanks, Stephane Chazelas, for clearing up the above behavior.) |
| 73 | |
| 74 | |
| 75 | # A more straightforward method is the ${!t} notation, discussed in the |
| 76 | #+ "Bash, version 2" section. |
| 77 | # See also ex78.sh. |
| 78 | |
| 79 | exit 0 |