askvity

How to Expand the PATH in Fish

Published in Fish Shell PATH 3 mins read

To expand the $PATH environment variable in the Fish shell, the recommended and simplest method is using the built-in function fish_add_path.

Using fish_add_path

The fish_add_path command is a convenient way to add new directory components to Fish's $PATH. This command is a simple way to add more components to fish's PATH, ensuring that executables located in those directories can be found and run from the terminal. It's designed to be safe and effective, even when used within configuration files like config.fish.

How it Works:

By default, fish_add_path adds the specified paths to the $fish_user_paths variable. Fish automatically includes the directories listed in $fish_user_paths in the main $PATH. This separation helps manage user-specific paths distinctly.

Alternatively, you can use the --path switch to add components directly to the $PATH variable instead of $fish_user_paths.

Why Use fish_add_path?

  • Simplicity: It abstracts away the details of managing the $PATH variable string.
  • Safety: It handles potential issues like duplicate entries and ensures the path is correctly structured. It is (by default) safe to use fish_add_path in config, meaning you can reliably put it in your config.fish file without causing issues on shell startup.
  • Integration: It works seamlessly with Fish's path management system, including the use of $fish_user_paths.

Examples

Adding a single directory to your path:

fish_add_path /usr/local/bin

Adding multiple directories:

fish_add_path /opt/mytool/bin ~/scripts

Adding a directory directly to the $PATH (less common for user configuration):

fish_add_path --path /path/to/temporary/tool

Where to Put fish_add_path

For persistent changes, you should add fish_add_path commands to your Fish configuration file, typically located at ~/.config/fish/config.fish. Fish executes this file when it starts, ensuring your desired paths are included in $PATH for every session.

Other Methods

While fish_add_path is the preferred Fish-native way, you could technically modify the $PATH variable directly like in other shells using the set command. However, this is generally not recommended for persistent configuration in Fish due to the benefits provided by fish_add_path and $fish_user_paths.

For example, adding a directory to the beginning of the PATH directly would look something like:

set PATH /new/directory $PATH

But again, fish_add_path is the idiomatic and safer approach in Fish.

Summary Table

Method Description Best Use Case Persistence
fish_add_path Adds to $fish_user_paths (default) or $PATH (--path). Configuration (config.fish) Persistent (in config)
set PATH ... Directly modifies the $PATH variable. Temporary session changes Not persistent unless in config (less recommended)

Expanding the $PATH in Fish is efficiently managed using the fish_add_path command, making it straightforward to ensure your system can find all necessary executables.

Related Articles