In the process of writing a shell script, it is believed that you will encounter this scenario:
#!/bin/sh#log_1= "xxxxxx" log_2= "yyyyyy" log_3= "zzzzzz" for num in {1..3}do log=log_$num #此时你想把log_1, log_2,log_3, these three variables The value of the variable is referenced in the For loop sequentially, but if you echo #出的变量 directly, you will only output the variable name of the variable you want to refer to, not the variable value of the variable, you need to use the shell #变量的间接引用了, you can compare the output in the script: Echo $log #输出的为变量的变量名 echo ${$log} #报错, where the error will be immediately after the exit, mainly to show you, we test the following several echo statements in advance to remove this statement or comment out. echo {!log} #成功 eval echo \$ $log #成功done
This article is from the "Red Mansions Dream" blog, please be sure to keep this source http://leidongya.blog.51cto.com/7375845/1577520
Indirect references to variables in the shell