'sh' environment does not respect the PATH extensions, user's local PATH not in effect?

纵饮孤独 提交于 2020-01-25 06:42:14

问题


$ bl 1
$ sh -c 'bl 1'
sh: bl: command not found

The bl script is located in the user's PATH extension (/home/user/.local/bin) but the sh environment does not seem to be aware of it, the bash is. The main /usr/bin/sh executable symlinks to /usr/bin/bash.

Placing a symlink in /usr/local/bin pointing to the local bl script does seem to fix the issue. Expanding the PATH manually $ PATH=/usr/bin:$HOME/.local/bin sh -c 'bl 1' also solves it, something I don't really understand since both the bash and the sh are aware of the PATH.

$ export -p |grep PATH=
declare -x PATH="/usr/local/sbin:/usr/local/bin:/usr/bin:~/.local/bin"
$ sh -c 'export -p |grep PATH'
export PATH="/usr/local/sbin:/usr/local/bin:/usr/bin:~/.local/bin"

"Something's missing and you have to find it" yet it's hard to look if you don't know what is missing.


回答1:


$ export -p |grep PATH
declare -x PATH="/usr/local/sbin:/usr/local/bin:/usr/bin:~/.local/bin"

Having a literal ~ is wrong. It should have been expanded to /home/user already. The shell will expand ~ when variables are assigned but not when they're expanded.

$ foo=~ && echo $foo     # expanded at assignment
/home/user
$ foo='~' && echo $foo   # not expanded since the assignment is quoted
~

Find the shell startup script where ~/.local/bin is added to the $PATH and make sure ~ is not quoted.

Wrong:

PATH="$PATH:~/.local/bin"

Right:

PATH=$PATH:~/.local/bin


来源:https://stackoverflow.com/questions/58912836/sh-environment-does-not-respect-the-path-extensions-users-local-path-not-in

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