Linux – Why does the dot command (source) not work on files without path in zsh

bashlinuxshellzsh

I recently switched from bash to zsh. In bash, I used the dot alias . for the source command, e.g.

. .bashrc

It worked with . .bashrc, . ~/.bashrc, . ./.bashrc.

However with zsh, the dot alias does not work in the same way. It only works with a path to the file. But not if I use a file without path:

This does not work:

. .zshrc

It would give me this error: .: no such file or directory: .zshrc

But these all work:

source .zshrc
. ./.zshrc
. ~/.zshrc

Best Answer

That's actually the standard behavior of the dot command: it uses a $PATH search, just like when running commands – not a regular relative path. So you can have your common libraries in e.g. ~/bin/ and simply load them with . libwhatever.sh from any directory.

Performing a direct relative path lookup is a nonstandard Bash extension.

(Similarly, dot . is actually the main command listed in the "shell language" standard, and source is a shell-specific alias – not the other way around.)

Related Question