익명 함수를 사용하여 변수의 범위를 제공 할 수 있습니다. 보낸 사람 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 -n
에 bash
너무 사용하여 "수출"속성은 변수에서 제거됩니다 export -n GREP_OPTIONS=--color=always
전혀 수출을 사용하지 동일합니다 - GREP_OPTIONS=--color=always
.
즉, 원하는 동작을 얻으려면을 사용하지 마십시오 export
. 대신,에 .zshrc
, 당신은해야한다
GREP_OPTIONS=--color=always
그러면 원하는대로 실행하는 모든 (대화식, 비 로그인) 셸에서 변수를 사용할 수 있지만 자식 셸로 내보내지는 않습니다.
export -n
내 보낸 변수를 내 보내지 않습니다.