bash 如何定义 shell 脚本变量以具有脚本之外的范围
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8153923/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How do I define a shell script variable to have scope outside of the script
提问by Obinwanne Hill
I'm using Ubuntu Natty.
我正在使用 Ubuntu Natty。
I have a shell script which I have saved to /etc/init.d/qstart. The shell script contains the following:
我有一个已保存到 /etc/init.d/qstart 的 shell 脚本。shell 脚本包含以下内容:
apt-get -y update
apt-get -y upgrade
apt-get -y install tofrodos gcc make nmap lsof expect sysstat
dbpass="mydbpassword"
However, after I execute the script, I want to test that dbpass is set and I enter echo $dbpassin the prompt. But it's empty.
但是,执行脚本后,我想测试是否设置了dbpass,然后echo $dbpass在提示中输入。但它是空的。
How do I define a variable in my shell script so that I can access it outside of it?!
如何在我的 shell 脚本中定义一个变量,以便我可以在它之外访问它?!
Thanks in advance.
提前致谢。
回答by Adam Zalcman
You can't set variables in parent process's environment. You can only set your current process's environment or prepare an environment for your process's children.
您不能在父进程的环境中设置变量。您只能设置当前进程的环境或为您的进程的子进程准备一个环境。
That said, you can instruct your shell to run commands from a script in the current shell process rather than forking a new shell. You can do it like this:
也就是说,您可以指示您的 shell 从当前 shell 进程中的脚本运行命令,而不是派生一个新的 shell。你可以这样做:
source your_script.sh
or
或者
. your_script.sh
(note the space after the dot). Since here commands inside your_script.share run by the current shell the changes made to the environment inside the script are retained.
(注意点后面的空格)。由于此处的命令your_script.sh由当前 shell 运行,因此保留对脚本内环境所做的更改。
However, if the shell running your script is not an ancestor of the shell in which you wish to use the environment variable then there is no way to achieve your goal using environment variables at all. For example, if you script is run at initialization by some childless shell all environment settings done there are irreversibly lost forever. In this case, use some other mechanism like a file (perhaps somewhere under /var).
但是,如果运行脚本的 shell 不是您希望在其中使用环境变量的 shell 的祖先,那么使用环境变量根本无法实现您的目标。例如,如果您的脚本在初始化时由某个无子 shell 运行,则所有完成的环境设置都将永远不可逆转地丢失。在这种情况下,请使用其他一些机制,例如文件(可能在 下的某处/var)。
If you want all instances of a given shell to have certain variables set in their environment you can use initialization scripts that most shells use. Usually, they have a system-wide and per-user initialization scripts. For example, bash uses /etc/profileas system-wide initialization script for interactive login shell and $HOME/.bash_profile(also $HOME/.bash_loginand $HOME/.profile) as per-user initialization script. See this referencefor bash-specific details. If you use a different shell, try its respective manual.
如果您希望给定 shell 的所有实例在其环境中设置某些变量,您可以使用大多数 shell 使用的初始化脚本。通常,它们具有系统范围和每个用户的初始化脚本。例如,bash/etc/profile用作交互式登录 shell 的系统范围的初始化脚本,并且$HOME/.bash_profile(也$HOME/.bash_login和$HOME/.profile)用作每个用户的初始化脚本。有关bash 特定的详细信息,请参阅此参考资料。如果您使用不同的外壳,请尝试其各自的手册。
回答by Michael Krelin - hacker
You can't set environment variable for parent process in the child process. What you can do is source script.shor . script.shinstead of executing it, in this case it's executed in the same bash process and all the changes are preserved.
您不能在子进程中为父进程设置环境变量。您可以做的是source script.sh或者. script.sh不执行它,在这种情况下,它是在同一个 bash 进程中执行的,并且所有更改都被保留。
Also, if you want to have the same variable set in your init script and elsewhere you may put it into /etc/default/something, which is more common. And finally you can sourcethis file into bashrcor /etc/profile.d/somethingfile.
此外,如果您想在您的 init 脚本和其他地方设置相同的变量,您可以将它放入 中/etc/default/something,这更常见。最后你可以把source这个文件放入bashrc或/etc/profile.d/something文件中。

