linux - 我可以将 source test1.sh 与 gnome-terminal -x 一起使用吗?

标签 linux bash shell

我正在使用源代码从 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/

相关文章:

linux - kubectl exec 的 Keepalive

bash - PostgreSQL 命令来自 bash 脚本

python - python 错误退出后未执行 Bash 陷阱

git - 在 git clone 之后删除不可见的目录

linux - kmalloc() kcalloc() vmalloc() 和 kzalloc() 之间有什么区别?

java - MakeFile 不工作?

linux - 无法将新文件夹名称附加到 UNIX 脚本中的现有路径

java - 带参数的运行时 exec 方法不会在 Java 中运行 bash 脚本

python - 替换文件名、文件夹名称和文件内容中出现的内容

linux - 使用 shell 脚本为每一行添加后缀