LDP/LDP/guide/docbook/abs-guide/ex78.sh

33 lines
730 B
Bash
Raw Normal View History

2001-07-10 14:25:50 +00:00
#!/bin/bash
# Indirect variable referencing.
# This has a few of the attributes of references in C++.
a=letter_of_alphabet
letter_of_alphabet=z
2001-09-04 13:27:31 +00:00
echo "a = $a" # Direct reference.
2001-07-10 14:25:50 +00:00
2001-09-04 13:27:31 +00:00
echo "Now a = ${!a}" # Indirect reference.
2008-11-23 22:43:47 +00:00
# The ${!variable} notation is more intuitive than the old
#+ eval var1=\$$var2
2001-07-10 14:25:50 +00:00
echo
t=table_cell_3
table_cell_3=24
2004-02-16 17:21:02 +00:00
echo "t = ${!t}" # t = 24
2001-07-10 14:25:50 +00:00
table_cell_3=387
2001-09-04 13:27:31 +00:00
echo "Value of t changed to ${!t}" # 387
2008-11-23 22:43:47 +00:00
# No 'eval' necessary.
2001-09-04 13:27:31 +00:00
2004-02-16 17:21:02 +00:00
# This is useful for referencing members of an array or table,
#+ or for simulating a multi-dimensional array.
2005-03-21 13:51:11 +00:00
# An indexing option (analogous to pointer arithmetic)
#+ would have been nice. Sigh.
2001-07-10 14:25:50 +00:00
exit 0
2008-07-20 23:16:47 +00:00
# See also, ind-ref.sh example.