38

我想在鱼启动时将 ./bin 目录(相对于当前 shell 目录)添加到 $PATH 。请注意,这fish是一个外壳。

echo $PATH
set PATH ./bin $PATH
echo $PATH

如果我将这些行放在~/.config/fish/config.fishshell 中,将回显相同的路径集合。绝对路径已正确添加。

如果我打开外壳并在包含它的set PATH ./bin $PATH某个目录中键入相同的内容,则会成功添加。bin但是,当当前目录中没有时,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。

4

6 回答 6

67

我发现持续添加路径$PATH的最佳方法是

set -U fish_user_paths $fish_user_paths ~/path/name

$PATH. _ 而且由于它是持久的,因此路径会$PATH在 shell 重新启动时保持不变。

config.fish它比在你的命令中修改你的更有效$PATH,因为它只运行一次,而不是在每次 shell 重新启动时运行。

该变量fish_user_paths旨在由用户1设置,如fish 的维护者荒谬的鱼所述。


为了方便起见,考虑创建一个鱼函数:2

# ~/.config/fish/functions/add_to_path.fish
function add_to_path --description 'Persistently prepends paths to your PATH'
  set --universal fish_user_paths $fish_user_paths $argv
end

并将其用作:

$ add_to_path foo bar  # Adds foo/ and bar/ to your PATH

笔记

  1. 在那个页面上作者给出了例子set -U fish_user_paths ~/bin。这将覆盖fish_user_paths单个值~/bin. 为避免丢失 中设置的现有路径fish_user_paths,请务必$fish_user_paths在添加的任何新路径之外包括(如我的回答中所示)。

  2. 我的 dotfiles 包含一个稍微高级一点的版本,它跳过添加重复项https://github.com/dideler/dotfiles/blob/master/.config/fish/functions/add_to_user_path.fish

于 2014-01-15T02:06:00.553 回答
16

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 that fish 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:

set PATH $PWD/bin $PATH

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.

于 2011-08-15T15:58:16.293 回答
11

我认为答案是使用set -U是一个红鲱鱼。相反,将以下内容添加到~/.config/fish/config.fish

if status --is-interactive
    set PATH $PATH ~/.local/bin;
end
于 2012-04-21T16:52:06.540 回答
7

direnv http://direnv.net/是一个很好的实用程序,可以帮助您完成正在做的事情。

通常,在 $PATH 前面加上 ./bin 是不安全的,因为任何对共享目录具有写访问权限的人都可以在例如 ./bin/ls 中隐藏恶意代码。当您在共享目录中运行 ls 时,该代码将执行。

direnv 不能解决这个问题(它基于 .envrc 文件工作,但任何人都可以放置这些文件),但至少当你 cd 进入 $PATH 正在修改的目录时,它会让你知道:

$ cd my_project
direnv: loading .envrc
direnv export: ~PATH
于 2013-12-28T21:12:16.203 回答
2

似乎fish不会将不存在的目录路径添加到 PATH。这也适用于相对路径。但是,如果您bin在主目录中创建目录,set PATH ./bin $PATH则每次启动时都会正常工作,因为它是从主目录执行的。不过,这是一种黑客行为。

于 2011-08-15T16:39:31.233 回答
0

.就个人而言,我认为添加的风险很小$PATH只要它是最后一项,因为之前不会发现lsCWD 中的流氓(或其他) 。/usr/bin/ls

我在我的添加这个config.fish

contains '.' $PATH; or set --export PATH $PATH .

你可以做类似的添加./bin,再次在最后一个位置:

contains './bin'; or set --export PATH $PATH ./bin

这里发生了两件事:

  1. 这是$PATH直接设置,就像其他 shell 一样。这不会在 shell 中持续存在。
  2. 这会检查它是否已经在 中$PATH,因此子外壳不会在$PATH
于 2021-10-03T07:10:25.677 回答