Search code examples
if-statementawkzsh

Why does this ZSH if-statement does not load with new console session?


I have this if-statment in my ZSH config file that does not load when a console session starts, but the rest of the file does.

then
alias sd="dirs -v | awk 'NR>1' | awk '!seen[$2]++'"
else
alias sd="dirs -v | awk 'NR>1'"
fi

Also, if I load the config file from within the session with exec zsh, it does work.

Any idea why?


Solution

  • Using an alias with double-quotes wrapping the outside, $2 is prone to being expanded. Beyond that, alias support is turned off by default in scripts.

    Consider a function:

    sd() { dirs -v | awk 'NR>1 && !seen[$2]++'; }