我正在使用源代码从 main 执行 2 个 shell 脚本。
main.sh
#/bin/sh
a=1
b=2
c=3
gnome-terminal -x sh -c ". ./test1.sh|less" (note the source ".")
gnome-terminal -x sh -c ". ./test2.sh|less"
...
...
test1.sh
#!/bin/sh
echo "a="$a #doesn't print anything
我能够分别执行以下 2,但是当我合并时,我无法将 main 的变量访问到其他文件中 1. gnome-terminal -x sh -c "test1.sh|less"#able to execute in separate terminal 2.. ./test1.sh #able to access from main.sh in test1.sh variables
最佳答案
这里有两个问题,第一个是你没有导出变量。
在这种情况下,您必须执行以下操作:
export a b c
设置变量后。
第二个问题是终端窗口将重新使用预先存在的 gnome 终端 session (如果存在)启动。这个预先存在的 session 将不知道这些环境变量。因此,您需要将选项 --disable-factory
传递给 gnome-terminal 命令,例如
gnome-terminal --disable-factory -x sh -c ". ./test1.sh|less"
然后您将在窗口中看到正确的值。
关于linux - 我可以将 source test1.sh 与 gnome-terminal -x 一起使用吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19129516/