17

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

1 Answer 1

15

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.)

6
  • 2
    Thanks, just out of curiosity, is there a way to have the "bash" behavior with zsh?
    – jost21
    Nov 5, 2019 at 16:02
  • 2
    Relative paths are accepted as long as they contain a slash, so . ./.bashrc would source from the current directory. The manual doesn't offer any option to exactly match bash behavior – you would have to either use source or put the current directory in $path (which is a poor idea).
    – user1686
    Nov 5, 2019 at 16:21
  • @jost21: If you're really, really want bash's behavior you could write a wrapper function, something like that function .() { [[ ! -f "$1" && -f "./$1" ]] && builtin . "./$1" || builtin . $@ } -- this (untested) snippet does not however take arguments into account.
    – mpy
    Nov 5, 2019 at 17:47
  • 1
    (It also doesn't account for the first . returning non-zero status and causing an unwanted fallback to the second .. Really, && || does not work the same way as a ternary operator.)
    – user1686
    Nov 5, 2019 at 18:13
  • @grawity: Oh yes, good point, thanks! I should have known better, but was too lazy to write if .. then .. else.
    – mpy
    Nov 5, 2019 at 19:34

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.