从bash脚本更改出口变量的值 [英] Changing the value of a export variable from a bash script

查看:94
本文介绍了从bash脚本更改出口变量的值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我没有从bash外壳下面。

I did the following from a bash shell.

出口mypath中=/ home / user中/目录

然后,我通过'echo'ing从两个壳这个值和bash脚本里验证了这个值。这两个工作的罚款。

Then I verified the value of this by 'echo'ing this value from both shell and a inside a bash script. Both worked fine.

然后我试图从这样的脚本中设置此变量的值。
mypath中=/ home / user中/ NEWDIR
并试图打印从外壳该变量。我以为变量会拥有更新的价值,但它显示了旧值。

Then I tried setting the value of this variable from inside a script like this. myPath="/home/user/newdir" and tried printing this variable from shell. I thought the variable will hold the updated value, but it was showing the old value.

我如何更新从脚本的价值?我期待这样做,而不使用如果可能的话。

How can I update the value from a script? I am looking to do it without using source if possible.

推荐答案

要进行持续的变量在脚本结束后,你必须使用的 命令:

To make the variables persist after the script finishes, you have to run it using the source command:

当脚本使用​​运行运行现有的外壳内,由脚本创建或修改的变量将在脚本完成后仍然可用。相反如果脚本就像文件名,然后一个独立的子shell(带有完全独立的变量)运行将催生运行该脚本。

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脚本更改出口变量的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆