Say we define the following shell function and alias in .zshenv:export VARIABLE_A='original'function my_function(){    print $VARIABLE_A}export VARIABLE_B='original'alias my_alias=print $VARIABLE_BNow, say that we start a new interactive shell, and we do the following:$ my_function$ export VARIABLE_A='new'$ my_function$ my_alias$ export VARIABLE_B='new'$ my_aliasThe commands above print:originalneworiginaloriginalIt looks like the function is updated, but the alias isn't. Is this behavior expected? And if so, why?
Updating shell functions and aliases through environment variables
shell script;zsh;environment variables
Yes, the behavior is expected. The reason being simply that you used double quotes () when defining the alias, allowing for parameter substitution. This led to $VARIABLE_B being substituted at the time of the definition of the alias instead of its execution.Essentially, when writingVARIABLE_B=originalalias my_alias=print $VARIABLE_Bzsh will replace $VARIABLE_B before running the alias command like so:alias my_alias=print originalTherefore the alias my_alias will always be replaced with print original before execution.If you want to substitute a parameter at execution time you have to quote it, either with single quotes ('') or by quoting $ with a \:alias my_alias='print $VARIABLE_B'alias my_alias=print \$VARIABLE_BThis will set the alias so, that the variable name remains part of the command and the alias my_alias is actually replaced with print $VARIABLE_B before execution.In contrast parameters inside a function definition do not need to be quoted explicitly, they are always only substituted at runtime. So when writingmy_function () {{    print $VARIABLE_A}this is exactly what will be stored. When running my_function $VARIABLE_A will be substituted with the value it has at that time.