简体   繁体   English

Shell 参数替换为 sh

[英]Shell parameter substitution with sh

In bash following command works where it use parameter substitution在 bash 中,以下命令在使用参数替换的情况下有效

$ VALUE_AUS=20m; COUNTRY=AUS; PARAM=VALUE_$COUNTRY; echo ${!PARAM}
20m

However in Alpine container with sh the same command fails但是在使用 sh 的 Alpine 容器中,相同的命令失败

# VALUE_AUS=20m; COUNTRY=AUS; PARAM=VALUE_$COUNTRY; echo ${!PARAM}
/bin/sh: syntax error: bad substitution

What would be the correct syntax to achieve same outcome using /bin/sh?使用 /bin/sh 实现相同结果的正确语法是什么?

Indirect variables is a bash extension.间接变量是bash扩展。

If Alpine can't run bash , you'll need to use eval to emulate this.如果 Alpine 无法运行bash ,您将需要使用eval来模拟这一点。

# VALUE_AUS=20m; COUNTRY=AUS; PARAM=VALUE_$COUNTRY; eval "echo \"\$${PARAM}\""

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

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