È possibile utilizzare una funzione anonima per fornire un ambito per la variabile. Da 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.
Ma a parte questo - se non si sta usando export
nel vostro .zshrc
a tutti, la variabile dovrebbe essere solo visibile nella vostra sessione interattiva corrente, e non dovrebbe essere esportato in sottoshell.
Come terdon spiegato nel suo commento: export -n
nel bash
solo fa sì che la proprietà "esportazione" di essere rimosso dalla variabile, in modo da utilizzare export -n GREP_OPTIONS=--color=always
equivale a non usare l'esportazione a tutti - GREP_OPTIONS=--color=always
.
In altre parole, per ottenere il comportamento desiderato, non usare export
. Invece, nel tuo .zshrc
, dovresti avere
GREP_OPTIONS=--color=always
Ciò renderà la variabile disponibile per tutte le shell (interattive, non di accesso) che si esegue, proprio come si desidera che sia, ma non verrà esportata in shell secondarie.
export -n
non esporta solo una variabile esportata.