zsh can neither find nor execute custom user scripts in ~/bin although they are executable and in the $PATH environment variable

こ雲淡風輕ζ 提交于 2020-06-17 09:45:13

问题


Problem:

I have recently upgraded from macOS Mojave 10.14.6 to macOS Catalina 10.5.5.

Now that zsh is the default shell, I migrated my .bashrc to .zshrc:

  • Aliases work fine.
  • But for some unexplainable reason, my custom user scripts in ~/bin/my/ and ~/bin/dev/ are
    • not completed by autocompletion
    • cannot be evoked when typed entirely, I then get zsh: command not found: myScript.sh

Investigation & Troubleshooting:

  • When changing the shell back to bash, everything still works, so data-loss/corruption from the OS update can be ruled out, it must be a configuration issue.
  • All my script files have the executable flags.
  • The directories they reside in are in the $PATH environment variable.
    • Proof: echo $PATH gives /opt/local/bin:/opt/local/sbin:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin:/Library/Apple/usr/bin:/usr/local/sbin:~/bin/bin:~/bin/my:~/bin/dev:

Experiments:

  • I tried setting/appending the $PATH variable with and without the export keyword. --> Makes no difference.
  • I tried the various possible config files: .zprofile .zshrc --> Makes no difference.
  • I hardlinked the bash and the zsh config file and included: alias test123="echo test123"
    • This alias works in bash and zsh. So it cannot be a problem in the chain of loading config files.
  • I tried different shebang syntaxes --> Makes no difference. Direct invocation with the full path works all the time anyhow.

回答1:


I found the solution in William Pursell's answer concerning Weird behaviour with zsh PATH:

Try using $HOME instead of ~. In many situations, shells do not expand ~ when you expect them to and it is usually better to use $HOME. ~ is really only intended to be a short cut for interactive use.

From then on zsh worked as expected and considered all my customer user scripts in ~/bin/* after I have replaced all my ~ tilde placeholders with $HOME variables in my $PATH. — What a great difference a wrong character can make!



来源:https://stackoverflow.com/questions/62251500/zsh-can-neither-find-nor-execute-custom-user-scripts-in-bin-although-they-are

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!