zsh中的局部变量:zsh中bash的“ export -n”等效项是什么


10

我正在尝试在zsh中将变量的范围包含到shell中,而没有孩子看到它。例如,我在.zshrc中键入:

GREP_OPTIONS=--color=always

但是,如果我使用以下命令运行shell脚本:

#!/bin/bash
echo $GREP_OPTIONS

输出为:

--color=always

虽然我希望它为null(上面的shell脚本根本不应该看到GREP_OPTIONS变量)。

用bash可以说:export -n GREP_OPTIONS=--color=always,这可以防止这种情况的发生。如何在zsh中完成此操作?


1
export -n只是取消导出导出的变量。
terdon

Answers:


11

export在zsh中,它是的简写typeset -gx,其中属性g表示“全局”(相对于函数的局部),而属性x表示“导出”(即,在环境中)。从而:

typeset +x GREP_OPTIONS

这也适用于ksh和bash。

如果您从未GREP_OPTIONS从头开始导出,则无需取消导出。

您还可以使用间接的,可移植的方式:取消设置变量会取消导出它。在ksh / bash / zsh中,如果变量为只读,则此方法无效。

tmp=$GREP_OPTIONS
unset GREP_OPTIONS
GREP_OPTIONS=$tmp

另请参见env -u GREP_OPTIONS your-script一些env实现(任何shell)。或者(unset GREP_OPTIONS; exec your-script)
斯特凡Chazelas

我尝试了排版+ x,但这也没有任何区别。如我的问题所示,某些东西正在导出我定义的所有变量,即使我不包括“ export”也是如此。仍在尝试找出原因。
PonyEars,2014年

@redstreet也许您不小心设置了export_all-a)选项?但是即使那样,typeset +x GREP_OPTIONS仍会取消导出变量。如果找不到问题,请尝试二进制搜索:备份您的文件.zshrc,删除后半部分,看问题是否仍然存在,然后追加第三季度或减少到第一季度并重复。
吉尔斯(Gilles)'所以

@Gilles谢谢,我发现了它:zsh具有我的.zshrc中具有的“ allexport”选项。如果可以帮助其他人,“ setopt noallexport”可以暂时将其关闭。因为它是最接近的,所以我会接受你的回答。
PonyEars,2014年

现在我有一个不同的问题,这更接近我试图解决的,问题就在这里:unix.stackexchange.com/questions/113645/...
PonyEars

6

您可以使用匿名函数来提供变量的作用域。来自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.

但是,从那个分开-如果你不使用export你的.zshrc所有,该变量只应在当前的交互式会话可见的,它不应该被导出到子shell。

正如terdon在他的评论中解释的那样:export -nin bash只会导致从变量中删除“ export”属性,因此using export -n GREP_OPTIONS=--color=always等于完全不使用export- GREP_OPTIONS=--color=always

换句话说,要获得所需的行为,请不要使用export。相反,在您的中.zshrc,您应该有

GREP_OPTIONS=--color=always

这将使变量对您运行的所有(交互式,非登录)shell都可用,就像您希望的那样,但不会将其导出到子shell。


只要有GREP_OPTIONS = - =颜色总是”是我在做什么,如在我的问题有些东西在的zsh导致我所有的变量自动导出仍在试图找出是什么。。
PonyEars
By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.