在多个终端调用多个函数

Dar*_*ary 6 command-line bash

我正在定义一个函数,我.bashrc用它来调用多个其他函数。

function startday {
    irene 
}
Run Code Online (Sandbox Code Playgroud)

这有效,(irene是一个定义和来源的函数)但是,我想在另一个终端中执行它。

function startday {
    gnome-terminal -c "irene"
}
Run Code Online (Sandbox Code Playgroud)

Failed to execute child process "irene" (No such file or directory)

irene激活一个 virtualenv,所以我希望终端在运行命令后不退出。(不用说,简单地irene在终端上执行确实能成功执行函数。)

aNe*_*ino 1

在我的 gnome 终端版本上

$ gnome-terminal --version
GNOME Terminal 3.22.0
Run Code Online (Sandbox Code Playgroud)

我没有选择-c,但我有选择-x-e

但是,如果您想开始运行 .bashrc 中gnome-terminal内部函数,您可以尝试这样做:

gnome-terminal -x "bash" -ic "irene"
Run Code Online (Sandbox Code Playgroud)

请注意,bash 的-i选项将确保您的.bashrc值被评估,因此irene函数可用。

另请注意irene函数完成操作后将gnome-terminal停止。您可以考虑在之后 irene运行一些命令,例如:

gnome-terminal -x "bash" -ic "irene; sleep 2;" # wait 2 seconds after "irene" stops
Run Code Online (Sandbox Code Playgroud)

或者

gnome-terminal -x "bash" -ic "irene; bash;" # run NEW instance of bash after "irene" stops
Run Code Online (Sandbox Code Playgroud)

如果 irene 函数设置了一些环境变量,则只有导出它们,它们才可以在这个新的 bash 实例中访问。

function irene() {
   # ...some code
   export variable="value of variable"
   # ...some code
}
Run Code Online (Sandbox Code Playgroud)

代替

function irene() {
   # ...some code
   variable="value of variable"
   # ...some code
}
Run Code Online (Sandbox Code Playgroud)