$PATH环境变量似乎无法识别

$PATH环境变量似乎无法识别

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 (PATH="~/bin:…"), otherwise it stands for itself. To prepend a directory to the current value of 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"(尽管您不需要exportwith ,PATH因为它已经在环境中)。在 zsh 中,除非变量可能为空,否则不需要双引号,但如果设置PATHin .profile,则由/bin/shor处理/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

相关内容