在 Fish 启动时添加 $PATH 的相对路径
我想在 Fish 启动时将 ./bin 目录(相对于当前 shell 目录)添加到 $PATH 中。请注意,fish
是一个 shell。
echo $PATH
set PATH ./bin $PATH
echo $PATH
如果我将这些行放在 ~/.config/fish/config.fish
中,shell 将回显相同的路径集合。绝对路径已正确添加。
如果我打开 shell 并在包含 bin
的某个目录中输入相同的 set PATH ./bin $PATH
,则会成功添加。但是,当当前目录中没有 bin
时,它会显示错误。
set: Could not add component ./bin to PATH.
set: Value too large to be stored in data type
我在 OS X Lion 上运行 Fish 1.23.1。
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
我发现持久添加路径到
$PATH
的最佳方法是在
$PATH
前面添加。由于它是持久的,因此在 shell 重新启动时该路径仍保留在$PATH
中。它比在
config.fish
中放入命令来修改$PATH
更有效,因为与每次 shell 重新启动时运行相比,它只运行一次。变量
fish_user_paths
旨在设置由用户1,如ridiculousfish,fish 的维护者。为了方便起见,请考虑创建一个fish函数:2
并将其用作:
注释
在该页面上,作者给出了示例
set -U Fish_user_paths ~/bin
。这会使用单个值~/bin
覆盖fish_user_paths
。为了避免丢失fish_user_paths
中设置的现有路径,除了添加的任何新路径(如我的答案中所示)之外,请务必包含$fish_user_paths
。我的点文件包含一个稍微更高级的版本,可以跳过添加重复项https://github.com/dideler/dotfiles/blob/master/.config/fish/functions/add_to_user_path.fish
The best way I have found to persistently add a path to your
$PATH
isThis prepends to
$PATH
. And since it's persistent, the path stays in$PATH
on shell restarts.It's more efficient than putting a command in your
config.fish
to modify your$PATH
, because it only runs once compared to running on every shell restart.The variable
fish_user_paths
is intended to be set by the user1, as stated by ridiculousfish, the maintainer of fish.Consider creating a fish function for convenience: 2
And use it as:
Notes
On that page the author gives the example
set -U fish_user_paths ~/bin
. This overwritesfish_user_paths
with a single value of~/bin
. To avoid losing existing paths set infish_user_paths
, be sure to include$fish_user_paths
in addition to any new paths being added (as seen in my answer).My dotfiles contain a slightly more advanced version that skips adding duplicates https://github.com/dideler/dotfiles/blob/master/.config/fish/functions/add_to_user_path.fish
在此之前我从未听说过
fish
。我刚刚安装了它,这样我就可以尝试一下(在意识到fish
是一个 shell 之前,删除了我在这里写的几段内容)。看起来
set PATH dir-name $PATH
是将目录添加到$PATH
的正确语法。但是,将相对目录名称添加到
$PATH
几乎肯定是一个坏主意,并且您的shell会在该目录不存在时向您发出警告,从而帮您一个忙。 (fish
的设计是为了用户友好。)使用绝对路径代替:
首先检查
$PWD/bin
是否存在,如果不存在则打印错误消息。至于“
set:值太大,无法存储在数据类型中
”消息,您是否可以多次将目录添加到您的$PATH
中?在添加目录之前应该有某种方法来检查目录是否已在$PATH
中。I'd never heard of
fish
before this. I just installed it so I could try it out (and deleted a few paragraphs I had written here before realizing thatfish
is a shell).It looks like
set PATH dir-name $PATH
is the right syntax to prepend a directory to$PATH
.But adding a relative directory name to
$PATH
is almost certainly a bad idea, and your shell is doing you a favor by warning you when the directory doesn't exist. (fish
is designed to be user-friendly.)Use an absolute path instead:
and first check whether
$PWD/bin
exists, printing an error message if it doesn't.As for the "
set: Value too large to be stored in data type
" message, could you be adding the directory to your$PATH
multiple times? There should be some way to check whether a directory is already in$PATH
before adding it.我认为答案是使用 set -U 是一种转移注意力的做法。相反,请将以下内容添加到
~/.config/fish/config.fish
:I think the answer is that using
set -U
is a red herring. Instead, add the following to~/.config/fish/config.fish
:direnv http://direnv.net/ 是一个很好的实用程序,可以帮助您完成正在做的事情。
一般来说,在 $PATH 前面加上 ./bin 是不安全的,因为任何对共享目录具有写访问权限的人都可能将恶意代码隐藏在 ./bin/ls 中。当您在共享目录中运行 ls 时,该代码将执行。
direnv 不能解决这个问题(它基于 .envrc 文件工作,但任何人都可以放置这些文件),但至少它让您知道当您 cd 到 $PATH 正在修改的目录时:
direnv http://direnv.net/ is a good utility to help with what you're doing.
Generally, prepending $PATH with ./bin is insecure, as anyone with write-access to a shared directory could hide malicious code in e.g. ./bin/ls. That code would execute when you run ls in the shared directory.
direnv does not solve this problem (it works based on .envrc files, but anyone could be placing those), but at the very least it makes you aware when you cd into a directory that $PATH is getting modified:
看起来
fish
不会将不存在的目录路径添加到 PATH 中。这也适用于相对路径。但是,如果您在主目录中创建bin
目录,set PATH ./bin $PATH
将在每次启动时正常工作,因为它是从主目录执行的。但这是一种黑客行为。It seems like
fish
won't add a non-existing directory path to PATH. That applies to relative paths too. But if you createbin
directory in your home directoryset PATH ./bin $PATH
will work properly on each startup since it is executed from home. This is kind of a hack though.就我个人而言,我认为将
.
添加到$PATH
中的风险很小,只要它是最后一项,因为流氓CWD 中的ls
(或其他内容)将不会在/usr/bin/ls
之前找到。我将其添加到我的
config.fish
中:您可以执行类似的操作来添加
./bin
,再次在 last 位置:有两件事正在发生在这里:
$PATH
,就像其他 shell 一样。这不会在 shell 中持续存在。$PATH
中,以便子 shell 不会在$PATH
中出现更长的冗余条目Personally, I think there is only a small risk in adding
.
to the$PATH
, so long as it's the last item, because a roguels
(or whatever) in the CWD will not be found before/usr/bin/ls
.I add this in my
config.fish
:You could do similar for adding
./bin
, again in the last position:Two things are going on here:
$PATH
directly, just as with other shells. This won't persist across shells.$PATH
, so that sub-shells won't get longer, redundant entries in$PATH