简体   繁体   English

在POSIX shell中,如何打印变量值的值

[英]in POSIX shell, how to print the value of the value of a variable

In a POSIX shell: 在POSIX Shell中:

>VALUE=value
>FOOBAR=VALUE

Now how to print the value of $FOOBAR ? 现在如何打印$FOOBAR的值? $$FOOBAR does not work - it goes left-to-right so interprets $$ first as process ID. $$FOOBAR不起作用-它从左到右,因此首先将$$解释为进程ID。

The duplicate question before me that people are pointing to, yes it does contain the answer to my question but it buried in a ton of irrelevant gunk. 人们要指向我的重复问题,是的,它确实包含了我的问题的答案,但它被埋在了一大堆无关紧要的东西中。 My question is much simpler and to the point. 我的问题要简单得多,而且要点。

For POSIX portability, you are pretty much left with 对于POSIX的可移植性,您几乎已经离开了

eval echo \$$variable

The usual caveats apply; 通常需要注意的事项; don't use eval if the string you pass in is not completely and exclusively under your own control. 如果您传入的字符串不是完全由您自己控制,则不要使用eval

I lowercased your example; 我小写了你的例子; you should not be using upper case for your private variables, as uppercase names are reserved for system use. 您不应将大写字母用作私有变量,因为大写字母名称保留供系统使用。

The only way to do this in POSIX is to use eval , so be very certain that FOOBAR contains nothing more than a valid variable name. 在POSIX做到这一点的唯一方法是使用eval ,所以肯定的是, FOOBAR包含无非就是一个有效的变量名。

$ VALUE=value
$ FOOBAR=VALUE
$ valid_var='[[:alpha:]_][[:alnum:]_]*$'
$ expr "$FOOBAR" : "$valid_var" > /dev/null && eval "echo \$$FOOBAR"

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM