macos 在 fish 启动时添加到 $PATH 的相对路径
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7064053/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Add a relative path to $PATH on fish startup
提问by Simon Perepelitsa
I want to add ./bin directory (which is relative to current shell directory) to $PATH on fish startup. Note that fish
is a shell.
我想在鱼启动时将 ./bin 目录(相对于当前 shell 目录)添加到 $PATH 。请注意,这fish
是一个外壳。
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.
如果我将这些行放在~/.config/fish/config.fish
shell 中,将回显相同的路径集合。绝对路径已正确添加。
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.
如果我打开 shell 并set PATH ./bin $PATH
在包含bin
它的某个目录中键入相同的内容,则会成功添加。但是,当bin
当前目录中没有时,它会向我显示错误。
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.
我在 OS X Lion 上运行 fish 1.23.1。
采纳答案by Simon Perepelitsa
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.
似乎fish
不会向 PATH 添加不存在的目录路径。这也适用于相对路径。但是,如果您bin
在主目录中创建目录,set PATH ./bin $PATH
则每次启动时都可以正常工作,因为它是从主目录执行的。不过,这有点像黑客。
回答by Dennis
The best way I have found to persistently add a path to your $PATH
is
我发现持续添加路径$PATH
的最佳方法是
set -U fish_user_paths $fish_user_paths ~/path/name
This prependsto $PATH
. And since it's persistent, the path stays in $PATH
on shell restarts.
这种预先考虑到$PATH
。而且由于它是持久的,所以路径在$PATH
shell 重新启动时保持不变。
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.
它比在您的 中添加命令config.fish
来修改您的 更有效$PATH
,因为与在每次 shell 重新启动时运行相比,它只运行一次。
The variable fish_user_paths
is intended to be set by the user1, as stated by ridiculousfish, the maintainer of fish.
该变量fish_user_paths
旨在由用户1设置,如fish 的维护者fakefish所述。
Consider creating a fish function for convenience: 2
为方便起见,考虑创建一个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
And use it as:
并将其用作:
$ add_to_path foo bar # Adds foo/ and bar/ to your PATH
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
在该页面上,作者给出了示例
set -U fish_user_paths ~/bin
。这将fish_user_paths
使用单个值覆盖~/bin
。为避免丢失 中设置的现有路径fish_user_paths
,请确保$fish_user_paths
在添加任何新路径之外还包括(如我的回答所示)。我的 dotfiles 包含一个稍微高级的版本,它跳过添加重复项https://github.com/dideler/dotfiles/blob/master/.config/fish/functions/add_to_user_path.fish
回答by Keith Thompson
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).
fish
在此之前我从未听说过。我刚刚安装了它,所以我可以尝试一下(并在意识到这fish
是一个 shell之前删除了我在这里写的几段)。
It looks like set PATH dir-name $PATH
is the right syntax to prepend a directory to $PATH
.
看起来set PATH dir-name $PATH
是将目录添加到$PATH
.
But adding a relativedirectory 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.)
但是,添加一个相对目录名$PATH
几乎肯定是一个坏主意,当目录不存在时,您的 shell 通过警告您来帮您一个忙。(fish
旨在方便用户。)
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.
并首先检查是否$PWD/bin
存在,如果不存在则打印错误消息。
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: Value too large to be stored in data type
”消息,您是否可以$PATH
多次将目录添加到您的目录中?$PATH
在添加目录之前,应该有一些方法可以检查目录是否已经存在。
回答by dhardy
I think the answer is that using set -U
is a red herring. Instead, add the following to ~/.config/fish/config.fish
:
我认为答案是使用set -U
是一种红鲱鱼。相反,将以下内容添加到~/.config/fish/config.fish
:
if status --is-interactive
set PATH $PATH ~/.local/bin;
end
回答by Florian D
direnv http://direnv.net/is a good utility to help with what you're doing.
direnv http://direnv.net/是一个很好的实用程序,可以帮助您完成正在做的事情。
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.
通常,在 $PATH 前面加上 ./bin 是不安全的,因为任何对共享目录具有写访问权限的人都可以将恶意代码隐藏在例如 ./bin/ls 中。当您在共享目录中运行 ls 时,该代码将执行。
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:
direnv 并没有解决这个问题(它基于 .envrc 文件工作,但任何人都可以放置这些文件),但至少它让你知道当你 cd 进入一个 $PATH 正在被修改的目录时:
$ cd my_project
direnv: loading .envrc
direnv export: ~PATH