![$PATH环境变量似乎无法识别](https://linux22.com/image/55399/%24PATH%E7%8E%AF%E5%A2%83%E5%8F%98%E9%87%8F%E4%BC%BC%E4%B9%8E%E6%97%A0%E6%B3%95%E8%AF%86%E5%88%AB.png)
I've made a fresh install of Debian Wheezy, and installed zsh to it. Few days after, I've done a vanilla installation of TeX Live 2014, so I added the necessary binary paths to my $PATH. Now I started writing little scripts so I would like to put them somewhere easily accessible, that is ~/bin
.
My path looks like this:
~/bin:/opt/texbin:/usr/local/bin:/usr/bin:/bin:/usr/local/games:/usr/games
Now, if I wanted to run something from TeX Live, it's easy:
% which pdflatex
/opt/texbin/pdflatex
No problem. But when I try running something from ~/bin
, ...
% which hello_world
hello_world not found
So I double-checked:
% ls -l ~/bin
total 18
-rwxr-xr-x 1 bozbalci bozbalci 5382 Sep 8 00:28 hello_world
And it shows that hello_world is doing fine in ~/bin
with its execution permissions set. I've tried rehash
, but it didn't work. Help?
答案1
In a shell command like
PATH=~/bin:/opt/texbin:/usr/local/bin:/usr/bin:/bin:/usr/local/games:/usr/games
the tilde is expanded to your home directory when the shell command is executed. Thus the resulting value of PATH
is something like
/home/theconjuring/bin:/opt/texbin:/usr/local/bin:/usr/bin:/bin:/usr/local/games:/usr/games
.
Make sure that the tilde isn't within quotes (), otherwise it stands for itself. To prepend a directory to the current value of PATH="~/bin:…"
PATH
, you can use
PATH=~/bin:$PATH
In general, in shells other than zsh, $PATH
outside double quotes breaks when the value contains spaces or other special characters,但在作业中,这是安全的。但是,对于export
,您需要编写export PATH=~/bin:"$PATH"
(尽管您不需要export
with ,PATH
因为它已经在环境中)。在 zsh 中,除非变量可能为空,否则不需要双引号,但如果设置PATH
in .profile
,则由/bin/sh
or处理/bin/bash
。
但是,如果您设置PATH
为~/.pam_environment
,则无法使用~
或$HOME
代表您的主目录。该文件不是由 shell 解析的,它是一个简单的NAME=value
行列表。所以你需要写完整的路径。
答案2
更新:巴什将要在您的 PATH 变量中展开 ~ 。但要注意,Python 不会。我艰难地发现了这一点。
$ export PATH="~/foo:$PATH"
$ type -a foo.sh
foo.sh is /home/myles/foo/foo.sh
Python 3.9.11 (main, Sep 2 2022, 07:10:47)
Type 'copyright', 'credits' or 'license' for more information
IPython 8.6.0 -- An enhanced Interactive Python. Type '?' for help.
In [1]: shutil.which('foo.sh')
None