Bash 在变量中扩展变量 [英] Bash expand variable in a variable

查看:29
本文介绍了Bash 在变量中扩展变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试设置我的 PS1 提示变量以动态选择颜色.为此,我定义了一堆带有颜色名称的局部变量:

$ echo $Green33[0;32m

但我希望在动态分配变量时使用它们,但我不知道如何正确扩展它们:

>colorstr="${$color}">回声 $colorstr${绿色}

我已经尝试了十几种 evalecho 和双引号的组合,但似乎都不起作用.扩展变量的逻辑方式(我认为)导致错误:

>colorstr="${$color}"-bash: ${$color}: 替换错误

(为了清楚起见,我使用 > 而不是 $ 作为提示字符,但我使用的是 bash)

如何扩展该变量?即,以某种方式将绿色"一词赋予值 33[0;32m?最好让 bash 或终端将 33[0;32m 解析为绿色.

我之前误用了 ${!x}eval echo $x,所以我接受了它们作为解决方案.对于(可能是病态的)好奇,函数和 PS1 变量在这个要点上:https://gist.github.com/4383597

解决方案

使用 eval 是经典的解决方案,但 bash 有更好的(更容易控制,更少的失误)-like) 解决方案:

  • ${!colour}

Bash (4.1) 参考手册 说:><块引用>

如果参数的第一个字符是感叹号(!),一个变量间接层介绍.Bash 使用由参数的其余部分形成的变量的值作为变量的名称;然后扩展此变量,并在其余部分使用该值的替代,而不是参数本身的值.这被称为间接扩展.

例如:

$ Green=$'33[32;m'$ echo "$Green" |ODX0x0000: 1B 5B 33 32 3B 6D 0A .[32;m.0x0007:$ 颜色=绿色$回声 $颜色绿$ echo ${!color} |ODX0x0000: 1B 5B 33 32 3B 6D 0A .[32;m.0x0007:$

(odx 命令非常不标准,只是简单地以十六进制格式转储其数据,右侧显示可打印字符.由于普通的 echo 没有显示任何东西,我需要看看回声是什么,我使用了我大约 24 年前写的一个老朋友.)

I'm trying to set up my PS1 prompt variable to dynamically choose a color. To do this, I've defined a bunch of local variables with color names:

$ echo $Green
33[0;32m

but I was hoping to use those in dynamically assigning variables, but I can't figure out how to expand them properly:

> colorstr="${$color}"
> echo $colorstr
${Green}

I've tried a dozen combinations of eval, echo, and double-quotes, and none seem to work. The logical way (I thought) to expand the variable results in an error:

> colorstr="${$color}"
-bash: ${$color}: bad substitution

(for clarity I've used > instead of $ for the prompt character, but I am using bash)

How can I expand that variable? i.e., somehow get the word "Green" to the value 33[0;32m? And prefereably, have bash or the terminal parse that 33[0;32m as the color green too.

EDIT: I was mis-using ${!x} and eval echo $x previously, so I've accepted those as solutions. For the (perhaps morbidly) curious, the functions and PS1 variable are on this gist: https://gist.github.com/4383597

解决方案

Using eval is the classic solution, but bash has a better (more easily controlled, less blunderbuss-like) solution:

  • ${!colour}

The Bash (4.1) reference manual says:

If the first character of parameter is an exclamation point (!), a level of variable indirection is introduced. Bash uses the value of the variable formed from the rest of parameter as the name of the variable; this variable is then expanded and that value is used in the rest of the substitution, rather than the value of parameter itself. This is known as indirect expansion.

For example:

$ Green=$'33[32;m'
$ echo "$Green" | odx
0x0000: 1B 5B 33 32 3B 6D 0A                              .[32;m.
0x0007:
$ colour=Green
$ echo $colour
Green
$ echo ${!colour} | odx
0x0000: 1B 5B 33 32 3B 6D 0A                              .[32;m.
0x0007:
$

(The odx command is very non-standard but simply dumps its data in a hex format with printable characters shown on the right. Since the plain echo didn't show anything and I needed to see what was being echoed, I used an old friend I wrote about 24 years ago.)

这篇关于Bash 在变量中扩展变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆