在 Fish 启动时添加 $PATH 的相对路径

发布于 2024-11-30 03:50:45 字数 536 浏览 1 评论 0 原文

我想在 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。

I want to add ./bin directory (which is relative to current shell directory) to $PATH on fish startup. Note that fish is a shell.

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

If I place these lines inside ~/.config/fish/config.fish the shell will echo the same collection of paths. Absolute paths are added properly.

If I open the shell and type the same set PATH ./bin $PATH inside some directory containing bin it is added successfully. However when there is no bin inside current directory it shows me an error.

set: Could not add component ./bin to PATH.
set: Value too large to be stored in data type

I'm running fish 1.23.1 on OS X Lion.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

如果没有 2024-12-07 03:50:45

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

set -U fish_user_paths $fish_user_paths ~/path/name

$PATH 前面添加。由于它是持久的,因此在 shell 重新启动时该路径仍保留在 $PATH 中。

它比在 config.fish 中放入命令来修改 $PATH 更有效,因为与每次 shell 重新启动时运行相比,它只运行一次。

变量 fish_user_paths 旨在设置由用户1,如ridiculousfish,fish 的维护者。


为了方便起见,请考虑创建一个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。这会使用单个值 ~/bin 覆盖 fish_user_paths。为了避免丢失 fish_user_paths 中设置的现有路径,除了添加的任何新路径(如我的答案中所示)之外,请务必包含 $fish_user_paths

  2. 我的点文件包含一个稍微更高级的版本,可以跳过添加重复项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 is

set -U fish_user_paths $fish_user_paths ~/path/name

This 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

# ~/.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

And use it as:

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

Notes

  1. On that page the author gives the example set -U fish_user_paths ~/bin. This overwrites fish_user_paths with a single value of ~/bin. To avoid losing existing paths set in fish_user_paths, be sure to include $fish_user_paths in addition to any new paths being added (as seen in my answer).

  2. 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

著墨染雨君画夕 2024-12-07 03:50:45

在此之前我从未听说过fish。我刚刚安装了它,这样我就可以尝试一下(在意识到 fish 是一个 shell 之前,删除了我在这里写的几段内容)。

看起来 set PATH dir-name $PATH 是将目录添加到 $PATH 的正确语法。

但是,将相对目录名称添加到$PATH几乎肯定是一个坏主意,并且您的shell会在该目录不存在时向您发出警告,从而帮您一个忙。 (fish 的设计是为了用户友好。)

使用绝对路径代替:

set PATH $PWD/bin $PATH

首先检查 $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 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.

沫离伤花 2024-12-07 03:50:45

我认为答案是使用 set -U 是一种转移注意力的做法。相反,请将以下内容添加到 ~/.config/fish/config.fish

if status --is-interactive
    set PATH $PATH ~/.local/bin;
end

I think the answer is that using set -U is a red herring. Instead, add the following to ~/.config/fish/config.fish:

if status --is-interactive
    set PATH $PATH ~/.local/bin;
end
旧情勿念 2024-12-07 03:50:45

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

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

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

$ cd my_project
direnv: loading .envrc
direnv export: ~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:

$ cd my_project
direnv: loading .envrc
direnv export: ~PATH
枕花眠 2024-12-07 03:50:45

看起来 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 create bin directory in your home directory set PATH ./bin $PATH will work properly on each startup since it is executed from home. This is kind of a hack though.

躲猫猫 2024-12-07 03:50:45

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

我将其添加到我的 config.fish 中:

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

您可以执行类似的操作来添加 ./bin,再次在 last 位置:

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

有两件事正在发生在这里:

  1. 这是直接设置 $PATH ,就像其他 shell 一样。这不会在 shell 中持续存在。
  2. 这会检查它是否已经存在于 $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 rogue ls (or whatever) in the CWD will not be found before /usr/bin/ls.

I add this in my config.fish:

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

You could do similar for adding ./bin, again in the last position:

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

Two things are going on here:

  1. This is setting $PATH directly, just as with other shells. This won't persist across shells.
  2. This checks that it's not already in the $PATH, so that sub-shells won't get longer, redundant entries in $PATH
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文