Vous pouvez utiliser une fonction anonyme pour fournir une portée à la variable. De 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.
Mais à part cela - si vous n'utilisez pas du tout export
dans votre .zshrc
, la variable ne devrait être visible que dans votre session interactive actuelle, et elle ne devrait pas être exportée vers des sous-coquilles.
Comme terdon l'a expliqué dans son commentaire: export -n
en fait bash
juste supprimer la propriété "export" de la variable, donc utiliser export -n GREP_OPTIONS=--color=always
équivaut à ne pas utiliser du tout l'exportation - GREP_OPTIONS=--color=always
.
En d'autres termes, pour obtenir le comportement souhaité, ne l'utilisez pas export
. Au lieu de cela, dans votre .zshrc
, vous devriez avoir
GREP_OPTIONS=--color=always
Cela rendra la variable disponible pour tous les shells (interactifs, sans connexion) que vous exécutez, tout comme vous le souhaitez, mais elle ne sera pas exportée vers les shells enfants.
export -n
juste exporte une variable exportée.