如何在linux中的单个命令中获取具有相同前缀的变量值?

How to get the variable values with same prefix in a single command in linux?

我有几个像

这样的环境变量
variable_a=xxx
variable_b=yyy
variable_c=zzz

我想将所有这些设置为另一个变量。我怎样才能通过参考前缀 variable_ 的单个命令来做到这一点 我知道,我们可以这样做 new_var=${variable_a} ${variable_b} ${variable_c}。 但是我需要知道我们是否可以使用前缀引用来做到这一点。

您可以在 bash 中使用特殊的 ${!var*} 扩展来获取设置变量名称的列表。然后你可以迭代它们并使用间接扩展来获取值。然后用空格连接值。

new_var=$(
      for i in ${!variable_*}; do
          printf "%s\n" "${!i}"
      done | paste -sd ' '
)