Puede usar una función anónima para proporcionar un alcance para 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.
Pero aparte de eso, si no está usando export
en .zshrc
absoluto, la variable solo debería ser visible en su sesión interactiva actual, y no debería exportarse a subcapas.
Como terdon explicó en su comentario: export -n
en bash
solo hace que la propiedad "exportar" se elimine de la variable, por lo que usar export -n GREP_OPTIONS=--color=always
es equivalente a no usar la exportación en absoluto - GREP_OPTIONS=--color=always
.
En otras palabras, para obtener el comportamiento deseado, simplemente no lo use export
. En cambio, en tu .zshrc
, deberías tener
GREP_OPTIONS=--color=always
Eso hará que la variable esté disponible para todos los shells (interactivos, sin inicio de sesión) que ejecute, tal como lo desea, pero no se exportará a shells secundarios.
export -n
simplemente exporta una variable exportada.