zsh의 지역 변수 : zsh에서 bash의 “export -n”에 해당하는 것은 무엇입니까 이것을 .zshrc에 입력하십시오. GREP_OPTIONS=–color=always 그러나 다음과

zsh에서 변수의 범위를 쉘에 포함시키고 자식이 보지 못하게하려고합니다. 예를 들어 이것을 .zshrc에 입력하십시오.

GREP_OPTIONS=--color=always

그러나 다음과 같이 쉘 스크립트를 실행하면 :

#!/bin/bash
echo $GREP_OPTIONS

출력은 다음과 같습니다.

--color=always

나는 그것이 null이되기를 원합니다 (위의 쉘 스크립트는 GREP_OPTIONS 변수를 전혀 보지 않아야합니다).

bash에서는 다음 export -n GREP_OPTIONS=--color=always과 같이 말할 수 있습니다 . zsh에서 어떻게이 작업을 수행합니까?



답변

exportzsh의 약어는 typeset -gx여기서 속성이 g“전역”을 의미하고 (함수와 로컬이 아닌) 속성이 x“내 보낸”(즉, 환경)을 의미합니다. 그러므로:

typeset +x GREP_OPTIONS

이것은 ksh와 bash에서도 작동합니다.

처음에 내보내기를하지 않으면 내보내기 GREP_OPTIONS를 해제 할 필요가 없습니다.

변수를 설정 해제하면 변수를 내 보내지 않고 간접적이고 이식 가능한 방법을 사용할 수도 있습니다. ksh / bash / zsh에서 변수가 읽기 전용 인 경우 작동하지 않습니다.

tmp=$GREP_OPTIONS
unset GREP_OPTIONS
GREP_OPTIONS=$tmp


답변

익명 함수를 사용하여 변수의 범위를 제공 할 수 있습니다. 보낸 사람 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모든에서, 변수는 현재 대화 형 세션에서 볼 수 있어야하고,이 서브 쉘에 수출 할 수 없습니다.

terdon 그의 주석에서 설명하고있는 바와 같이 : export -nbash너무 사용하여 “수출”속성은 변수에서 제거됩니다 export -n GREP_OPTIONS=--color=always전혀 수출을 사용하지 동일합니다 – GREP_OPTIONS=--color=always.

즉, 원하는 동작을 얻으려면을 사용하지 마십시오 export. 대신,에 .zshrc, 당신은해야한다

GREP_OPTIONS=--color=always

그러면 원하는대로 실행하는 모든 (대화식, 비 로그인) 셸에서 변수를 사용할 수 있지만 자식 셸로 내보내지는 않습니다.


답변