gpt4 book ai didi

bash - 在 shell 脚本之间导出变量

转载 作者:行者123 更新时间:2023-11-29 08:59:29 25 4
gpt4 key购买 nike

我有两个独立运行的脚本。第一个假设脚本 A 计算了一些值。我想从其他名为 B 的脚本中回显这些值。这些脚本不会相互调用。我使用了 export 关键字但没有用。我该怎么做?

最佳答案

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

假设这是您的 script1.sh

#!/bin/bash
# script1.sh: do something and finally
export x=19

这是我们的 script2.sh

#!/bin/bash
# script2.sh: read value of $x here
echo "x="${x}

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

(. ./script1.sh && ./script2.sh)

输出:

x=19

关于bash - 在 shell 脚本之间导出变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5677446/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com