linux bash: 当用 -e 打开 xterm 时,变量没有被定义

linux bash: when opening xterm with -e, the varaibles are not getting defined

我正在尝试一个简单的 xterm 命令:

xterm -hold -e "hare=0;echo $hare"

它将以空白打开 xterm

我观察到变量 hare 没有在 -e 引号内定义

其中:

hare=0;xterm -hold -e "echo $hare"

它打开一个显示 0

的 xterm

我必须在 -e 引号中定义变量并做一些事情。可不可以。我希望 xterm 访问外部变量以及 -e

中定义的变量

变量在双引号字符串中扩展,因此变量被原始 shell 扩展,而不是 xterm 中的 shell。使用单引号。

xterm -hold -e 'hare=0; echo $hare'

如果您在原始 shell 中分配变量,则需要将其导出,以便它成为子进程继承的环境变量:

hare=0
export hare
xterm -e 'echo $hare'