Skip to main content
The 2024 Developer Survey results are live! See the results

You are not logged in. Your edit will be placed in a queue until it is peer reviewed.

We welcome edits that make the post easier to understand and more valuable for readers. Because community members review edits, please try to make the post substantially better than how you found it, for example, by fixing grammar or adding additional resources and hyperlinks.

8
  • The quotes will explain why OP has to "run it twice for it to work". The first time, the directory is an argument to ~/.zshrc as it gets sourced, so when the alias gets redefined during that, the $@ in the alias expands to the directory. So the second time the alias does have cd <whatever-path>, and it works.
    – muru
    Commented May 14, 2021 at 7:51
  • Thanks. I've updated my answer. Commented May 14, 2021 at 8:08
  • Why should you not source ~/.zshrc? Opening a new terminal will source it anyway, so why would there be an issue if you source it manually?
    – terdon
    Commented May 14, 2021 at 9:32
  • @terdon Because it's been sourced already when your shell started up and the order in which commands are run can matter. As a simple example, put ls() { command ls "$@" | less }; alias ls='ls -AF' into your .zshrc file, restart your shell, then source ~/.zshrc. Now you get an error zsh: defining function based on alias 'ls'. And that's only a fairly innocuous example. Things can get much more hairy than that, especially when your .zshrc in turn sources 3rd-party files. Commented May 14, 2021 at 9:39
  • That doesn't actually give me an error in zsh. But even if you can find some edge case that does, I am surprised you seem so adamant against this. I have been changing my ~/.bashrc and then sourcing it for many years and have never once encountered a problem. Maybe I have been lucky, but it seems more likely that what you are describing isn't a very common problem. Or perhaps it is special to zsh? Come teach me here: Is there any reason to avoid sourcing the shell's configuration file?
    – terdon
    Commented May 14, 2021 at 9:55