gpt4 book ai didi

bash - 从 bash 脚本更改导出变量的值

转载 作者:行者123 更新时间:2023-12-02 01:14:03 26 4
gpt4 key购买 nike

我在 bash shell 中执行了以下操作。

export myPath="/home/user/dir"

然后我通过从 shell 和 bash 脚本内部“回显”这个值来验证这个值。两者都很好。

然后我尝试从这样的脚本中设置这个变量的值。myPath="/home/user/newdir"并尝试从 shell 打印这个变量。我以为该变量将保存更新后的值,但它显示的是旧值。

如何从脚本更新值?如果可能的话,我希望在不使用 source 的情况下做到这一点。

最佳答案

要使变量在脚本完成后保持不变,您必须使用 source 运行它命令:

When a script is run using source it runs within the existing shell, any variables created or modified by the script will remain available after the script completes. In contrast if the script is run just as filename, then a separate subshell (with a completely separate set of variables) would be spawned to run the script.

关于bash - 从 bash 脚本更改导出变量的值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13796809/

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