ind-ref.sh

来自「一本完整的描述Unix Shell 编程的工具书的所有范例」· Shell 代码 · 共 48 行

SH
48
字号
#!/bin/bash# ind-ref.sh: Indirect variable referencing.# Accessing the contents of the contents of a variable.a=letter_of_alphabet   # Variable "a" holds the name of another variable.letter_of_alphabet=zecho# Direct reference.echo "a = $a"          # a = letter_of_alphabet# Indirect reference.eval a=\$$aecho "Now a = $a"      # Now a = zecho# Now, let's try changing the second-order reference.t=table_cell_3table_cell_3=24echo "\"table_cell_3\" = $table_cell_3"            # "table_cell_3" = 24echo -n "dereferenced \"t\" = "; eval echo \$$t    # dereferenced "t" = 24# In this simple case, the following also works (why?).#         eval t=\$$t; echo "\"t\" = $t"echot=table_cell_3NEW_VAL=387table_cell_3=$NEW_VALecho "Changing value of \"table_cell_3\" to $NEW_VAL."echo "\"table_cell_3\" now $table_cell_3"echo -n "dereferenced \"t\" now "; eval echo \$$t# "eval" takes the two arguments "echo" and "\$$t" (set equal to $table_cell_3)echo# (Thanks, Stephane Chazelas, for clearing up the above behavior.)# Another method is the ${!t} notation, discussed in "Bash, version 2" section.# See also ex78.sh.exit 0

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?