shell-访问字符串同名变量

考虑以下场景:
期望通过给定的变量名称var_str,打印出该名称对应的变量值${var_str}。使用指令eval可以很方便的实现:

1
2
3
var_str="1213";
ned_param_name="var_str";
eval echo '$'"${ned_param_name}";

输出结果为1213;

eval命令解释如下:

1
2
3
4
5
6
7
eval [arg ...]
    The  args  are read and concatenated together into a single command. 
    This command is then read and executed by the shell, and its exit status is returned as
the value of eval.  If there are no args, or only null arguments, eval returns 0.
eval [参数 ...]
    参数将会被读取并作为一个指令被读入。然后这个指令将会被shell读取并执行,执行结果
将会作为eval的结果。如果没有参数传入,或者只有空参数,eval指令将会返回0。

对于上述的例子,echo $var_str将会被读入,并被shell重新执行。输出结果为1213。该结果即作为eval的输出结果。

Licensed under CC BY-NC-SA 4.0
Hello, World!
使用 Hugo 构建
主题 StackJimmy 设计