1

Unless I switch back to bash, I now have to spell out the full path for scripts I have written. How does zsh find commands?

Things in /bin, /use/bin, etc. still work, but things in ~/bin are “not found” even when that path is at the beginning of both $PATH and $path.

After putting

PATH="~/bin:$PATH
path="$PATH"

in ~/.zshrc,

echo $PATH; echo $path

shows it at the beginning of the path, but it still doesn't find the commands. (And it's not permissions: the scripts work when I type their full paths.)

7
  • How did you check the contents of your PATH environment variable? (i.e. did you check from a zsh prompt? - etc)
    – jksoegaard
    Nov 6, 2019 at 17:21
  • What happens if you type- typeset -U path and then path=(~/bin $path) ? I would probably add this in the file ~/.zprofile.
    – fd0
    Nov 6, 2019 at 17:45
  • I typically use $HOME/bin instead of ~/bin as it avoids typically errors, like quoting "~/bin", which doesn't work while "$HOME/bin" does. Nov 6, 2019 at 19:19
  • @user3439894, I'll try that. ~/bin works in bash, though. Apparently the claims of backwards compatibility for zsh are greatly exaggerated.
    – WGroleau
    Nov 6, 2019 at 20:26
  • Can you add the result of the echo and copy/paste the full error message you get?
    – nohillside
    Nov 6, 2019 at 21:47

2 Answers 2

2

zsh doesn't expand ~ within quotes. You need to use

PATH=~/bin:$PATH

instead.

PS: And path=$PATH is not required, zsh does this automatically

7
  • Well, either I left out the quotes or it did expand. As I said, both variables did start with ~/bin yet scripts there had to be spelled out fully.
    – WGroleau
    Nov 6, 2019 at 20:24
  • @Wgroleau If the path variable contains ~ it will not work
    – nohillside
    Nov 6, 2019 at 21:46
  • @WGroleau Does your path start with "~/bin", or with "/Users/yourname/bin"? The first one will not work; the second should. Nov 6, 2019 at 22:39
  • The first one will not work in zsh? That's a step backward: it did work in bash. But indeed, that silly limitation was the problem. I like Apple, but I often get irritated at the constant changes in appearance and other things with no obvious benefit to justify the re-learning.
    – WGroleau
    Nov 7, 2019 at 0:03
  • @WGroleau I think you're bit quick in judging there. You can't say that a jump from a 10 year old bash to a completely up to date zsh is "with no benefit". You'll get tonnes of features. Apple abandoned bash due to licensing issues (i.e. bash switched to GPLv3).
    – jksoegaard
    Nov 7, 2019 at 0:22
1

You do not list how exactly you checked that the path is actually at the beginning of $PATH - but generally you should be aware that macOS Catalina changed the default shell from bash to zsh - and as such you'll need to move over any modifications you've made to PATH in .bashrc (for example) to the corresponding file for zsh (for example .zshrc, .zshenv or .zprofile).

The "standard" way of adding paths to macOS is to create a file at /etc/paths.d/ containing the path to the directory you want to add to the path. Then when you launch any shell, it should automatically be added to your PATH environment variable. For example create a file called:

/etc/paths.d/wgroleau

And make it contain just the following line of text:

/Users/wgroleau/bin

This would system wide change default paths to include that directory.

UPDATE: Following your comments I'm starting to think that your problem might not be PATH-related at all. You state that the problems you experience revolve around scripts that you have written yourself. If those scripts happens to be bash-scripts that start of with a shebang for bash, then those scripts needs to be converted to zsh to work if you haven’t got bash installed - or alternatively you could install bash again from HomeBrew or from Apple.

9
  • I am aware of that change in Catalina, hence the question. I have edited to clarify that for reason unknown, your first suggestion doesn't work. I will try the system suggestion; it's something I hadn't heard of.
    – WGroleau
    Nov 6, 2019 at 19:11
  • @WGroleau Might be worth noting that changes to /etc/paths.d work system-wide, for all users/accounts
    – nohillside
    Nov 6, 2019 at 19:15
  • @nohillside Yes, that's already noted at the end of my answer. If you don't want it to be systemwide, use .zshrc or .zprofile in your home directory.
    – jksoegaard
    Nov 6, 2019 at 21:35
  • @WGroleau Checkout the update to my answer - could this be it?
    – jksoegaard
    Nov 6, 2019 at 21:38
  • Some of my scripts do specify bash. So they should continue to work because they should execute in bash as always. But the problem is not that they didn't work, but that zsh failed to find them in ~/bin, when ~/bin was the first item in the path.
    – WGroleau
    Nov 7, 2019 at 0:03

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .