たまにしかやらないのですが、毎回忘れるのでメモします。
結論
一時的な設定
setコマンドと-xオプションを使います。
/usr/local/binを追加するには、次のコマンドを実行します。
set -x PATH /usr/local/bin $PATH
永続的な設定
二つの方法があります。
方法1 設定ファイルを使う
他のshellと似た方法です。
/usr/local/binと/usr/sbinを追加するには、~/.config/fish/config.fishに
set PATH /usr/local/bin /usr/sbin $PATH
と書きます。
セッションを起動するたびに環境変数が設定されます。
方法2 fish_user_pathsユニバーサル変数に設定
fish shellではユニバーサル変数という、すべてのセッションで共通の変数を設定できます。
PATHを設定するときは、fish_user_paths変数を使います。
/usr/local/binを追加するには、次のコマンドを実行します。
set -U fish_user_paths /usr/local/bin $fish_user_paths
個人的に「なんとなくfish shellっぽい」という理由で、この方法を好んで使っています。
消すときは、次のように/usr/local/binを取り除いた文字列をfish_user_paths変数に設定しなおします。
set -U fish_user_paths (string match -v /usr/local/bin $fish_user_paths)
説明
fish: Tutorialに書いてあります。
$PATH
$PATHis an environment variable containing the directories in which fish searches for commands. Unlike other shells,$PATHis a list, not a colon-delimited string.To prepend /usr/local/bin and /usr/sbin to $PATH, you can write:
> set PATH /usr/local/bin /usr/sbin $PATHYou can do so directly in config.fish, like you might do in other shells with .profile. See this example.
A faster way is to modify the
$fish_user_pathsuniversal variable, which is automatically prepended to$PATH. For example, to permanently add /usr/local/bin to your$PATH, you could write:
> set -U fish_user_paths /usr/local/bin $fish_user_pathsThe advantage is that you don't have to go mucking around in files: just run this once at the command line, and it will affect the current session and all future instances too. (Note: you should NOT add this line to config.fish. If you do, the variable will get longer each time you run fish!)