在shell脚本之间导出变量

the*_*ux4 6 bash shell scripting

我有两个独立运行的脚本.第一个让我们说脚本A计算一些值.我想从其他名为B的脚本中回显这些值.这些脚本不会互相调用.我使用了export关键字但没有用.我怎样才能做到这一点?

anu*_*ava 6

如果我理解了这个要求,那么两个脚本都不能简单地在同一个子shell中执行,而是相互调用或者不需要像这样的外部文件或管道:

假设这是你的script1.sh

#!/bin/bash
# script1.sh: do something and finally
export x=19
Run Code Online (Sandbox Code Playgroud)

在这里我们你的script2.sh

#!/bin/bash
# script2.sh: read value of $x here
echo "x="${x}
Run Code Online (Sandbox Code Playgroud)

只需像这样在同一个子shell中调用它们

(. ./script1.sh && ./script2.sh)
Run Code Online (Sandbox Code Playgroud)

OUTPUT:

x=19
Run Code Online (Sandbox Code Playgroud)