Pon*_*ars 10 zsh shell-script environment-variables
我试图在 zsh 中将变量的范围包含在 shell 中,并且没有孩子看到它。例如,我在 .zshrc 中输入:
GREP_OPTIONS=--color=always
Run Code Online (Sandbox Code Playgroud)
但是,如果我使用以下内容运行 shell 脚本:
#!/bin/bash
echo $GREP_OPTIONS
Run Code Online (Sandbox Code Playgroud)
输出是:
--color=always
Run Code Online (Sandbox Code Playgroud)
虽然我希望它为空(上面的 shell 脚本根本不应该看到 GREP_OPTIONS 变量)。
在 bash 中,可以说: export -n GREP_OPTIONS=--color=always,这将防止这种情况发生。我如何在 zsh 中完成此操作?
Gil*_*il' 11
export在 zsh 中是 的简写typeset -gx,其中属性g表示“全局”(与函数的局部相反),属性x表示“导出”(即在环境中)。因此:
typeset +x GREP_OPTIONS
Run Code Online (Sandbox Code Playgroud)
这也适用于 ksh 和 bash。
如果您一开始从不导出GREP_OPTIONS,则无需取消导出。
您还可以使用间接的、可移植的方式:取消设置变量将其取消导出。在 ksh/bash/zsh 中,如果变量是只读的,这将不起作用。
tmp=$GREP_OPTIONS
unset GREP_OPTIONS
GREP_OPTIONS=$tmp
Run Code Online (Sandbox Code Playgroud)
您可以使用匿名函数为变量提供范围。来自man zshall:
ANONYMOUS FUNCTIONS
If no name is given for a function, it is `anonymous' and is handled
specially. Either form of function definition may be used: a `()' with
no preceding name, or a `function' with an immediately following open
brace. The function is executed immediately at the point of definition
and is not stored for future use. The function name is set to
`(anon)'.
Arguments to the function may be specified as words following the clos?
ing brace defining the function, hence if there are none no arguments
(other than $0) are set. This is a difference from the way other func?
tions are parsed: normal function definitions may be followed by cer?
tain keywords such as `else' or `fi', which will be treated as argu?
ments to anonymous functions, so that a newline or semicolon is needed
to force keyword interpretation.
Note also that the argument list of any enclosing script or function is
hidden (as would be the case for any other function called at this
point).
Redirections may be applied to the anonymous function in the same man?
ner as to a current-shell structure enclosed in braces. The main use
of anonymous functions is to provide a scope for local variables. This
is particularly convenient in start-up files as these do not provide
their own local variable scope.
For example,
variable=outside
function {
local variable=inside
print "I am $variable with arguments $*"
} this and that
print "I am $variable"
outputs the following:
I am inside with arguments this and that
I am outside
Note that function definitions with arguments that expand to nothing,
for example `name=; function $name { ... }', are not treated as anony?
mous functions. Instead, they are treated as normal function defini?
tions where the definition is silently discarded.
Run Code Online (Sandbox Code Playgroud)
但除此之外 - 如果您根本不使用exportin 您.zshrc的变量,则该变量应该只在您当前的交互式会话中可见,并且不应导出到子 shell。
正如 terdon 在他的评论中解释的那样:export -ninbash只会导致从变量中删除“export”属性,因此使用export -n GREP_OPTIONS=--color=always等同于根本不使用 export - GREP_OPTIONS=--color=always。
换句话说,要获得所需的行为,请不要使用export. 相反,在你的.zshrc,你应该有
GREP_OPTIONS=--color=always
Run Code Online (Sandbox Code Playgroud)
这将使该变量可用于您运行的所有(交互式、非登录)shell,就像您希望的那样,但它不会导出到子 shell。