First of all, aliases are not functions and do not accept arguments. Rather, aliases are simple substitutions: When you type goto foo
on the command line and press Enter, the shell simply replaces goto
with the value of the alias, before executing the command line.
Therefore, $@
does not expand to the arguments passed to your alias. In fact, in this case, it expands to the arguments passed to shell or the script you're sourcing. This is why goto
works correctly the second time: You have now passed a directory as an argument when sourcing your .zshrc
file and $@
will expand to that argument when you declare the alias again.
Additionally, since you're using "
double quotes"
, $@
expands immediately when you declare the alias. JustTo see that this is the case, right after your shell has started up, type alias
without arguments to view your aliases right after your shell has started up and you'll see that this is the case.
If you want goto
to be able to handle arguments, you should declare it as a function: goto() { cd $@ && ... }
However, as a rule, ⚠️you should not source ~/.zshrc
. Depending on what's in your dotfiles, this can lead to all sorts of problems.
Instead, if you've made changes to your .zshrc
file and you want to apply them, you should either restart your terminal (by closing the current tab/window and opening a new one) or restart the shell (by typing exec $SHELL
).