在 bash 中将变量作为本地变量
我有一个 bash 文件,其中定义了许多变量:
VAR1="value1"
VAR2="value2"
# ...
我需要在 ~/.bashrc
中导入这些变量,以便自定义 PS1、
PATH
等,但不希望导入的变量可以在 ~/.bashrc
之外访问。
为了让自己清楚地举个例子,我想做这样的事情:
function setPATH
{
local . ~/bashvars.sh # this isn't legal, of course...
PATH="$PATH:$VAR1" # $VAR1 is defined in ~/bashvars.sh
unset -f setPATH
}
setPATH
我怎样才能做到这一点?
I've got a bash file where are defined a number of variables:
VAR1="value1"
VAR2="value2"
# ...
I need to import these variables in ~/.bashrc
in order to customize PS1
, PATH
and so on, but don't want that the imported variables can be accessed outside of ~/.bashrc
.
To make myself clear with an example, I'd like to do something like this:
function setPATH
{
local . ~/bashvars.sh # this isn't legal, of course...
PATH="$PATH:$VAR1" # $VAR1 is defined in ~/bashvars.sh
unset -f setPATH
}
setPATH
How can I do this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
使用子外壳:
use a subshell: