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

How to open xterm -e 'command', preserving already declared functions?

我想运行以下命令:

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

returns: testfunction command not found (in the new xterm window).

但是当我在主终端调用函数时,它 returns 123

$ testfunction
123

尝试过

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

试图声明一个变量:

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

returns: 123 (in new xterm).

为什么新的 oppened xterm 找不到已声明的函数,但找到已声明的变量?

您需要导出 functions/variables 让子进程访问它们。

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

而且,xterm -hold -e "echo $variable"实际上并没有起作用,它只是看起来像这样。 $variable 用双引号括起来,因此在调用 xterm 之前展开.