So let's say I set a bunch of variables in my .zshrc:
function print_color () { echo -ne "%{\e[38;05;${1}m%}"; } # color guide: http://misc.flogisoft.com/_media/bash/colors_format/256-colors.sh.png RED=`print_color 160` DEFAULT="%{$fg[default]%}" PROMPT='$RED%${DEFAULT} $ '
The problem is these remain set, once I am actually running commands
$ echo $RED %{%} # With colors, etc.
How would I unset them after use in my .zshrc? What is the best practice here?
Thanks, Kevin
To unset an environment variable from Command Prompt, type the command setx variable_name “”. For example, we typed setx TEST “” and this environment variable now had an empty value.
Unsetting a parameter is done with the built-in unset
unset PARAMETER
unsets PARAMETER
. This can be done as soon as you no longer need PARAMETER
, at the end of the file or anywhere inbetween.
"Best practice" depends highly on the use case:
~/.zshrc
. Also, if you need a parameter only inside a function, you can declare it with local
function foo () { local PARAMETER # [...] }
It will then be only available inside this function without the need for unset
.
That being said, in this case you actually need RED
to be available in your running shell as it is needed each time your prompt is evaluated (everytime just before it is printed). This is due to PROMPT being defined in single quotes (and the shell option PROMPT_SUBST
being set, see output of setopt | grep promptsubst
).
If you do not intend to change RED
during runtime just put it in double quotes when defining PROMPT:
PROMPT="$RED"'%${DEFAULT} $ '
This will substitute $RED
when PROMPT
is defined and only ${DEFAULT}
each time PROMPT
is evaluated.
After that you can just do unset RED
. Note that there must be no $
before RED
, else the shell would substitute it by the value of RED
and try to unset a parameter named like the value:
% FOO=BAR ; BAR=X % echo "> $FOO | $BAR <" > BAR | X < % unset $FOO % echo "> $FOO | $BAR <" > BAR | < % unset FOO % echo "> $FOO | $BAR <" > | <
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With