如何打开xterm -e'命令',保留已经声明的函数?

问题描述 投票:0回答:1

我想运行以下命令:

$ testfunction (){ echo 123;}
$ xterm -hold -e "testfunction"

返回:找不到testfunction命令(在新的xterm窗口中)。

但是当我在主终端中调用该函数时,它返回123

$ testfunction
123

试着

declare -F | grep testfunction中,我可以看到函数已声明。

试图宣布只是一个变量:

$ variable='123'
$ xterm -hold -e "echo $variable"

返回:123(在新的xterm中)。

为什么新的oppened xterm没有找到声明的函数,但是找到了声明的变量?

linux bash function xterm
1个回答
2
投票

您需要导出函数/变量以允许子进程访问它们。

testfunction() { echo 123; }
export -f testfunction
xterm -hold -e "testfunction"

result

而且,xterm -hold -e "echo $variable"实际上并不起作用,它看起来就是这样。 $variable是双引号,因此在调用xterm之前扩展,即它的值传递给xtermxterm -hold -e 'echo $variable'不起作用,因为variable没有出口。

© www.soinside.com 2019 - 2024. All rights reserved.