Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Add a relative path to $PATH on fish startup

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.

like image 828
Simon Perepelitsa Avatar asked Aug 15 '11 10:08

Simon Perepelitsa


People also ask

How do you put a path on fish shell?

It does this by adding the components either to $fish_user_paths or directly to $PATH (if the --path switch is given). It is (by default) safe to use fish_add_path in config. fish, or it can be used once, interactively, and the paths will stay in future because of universal variables.

How do I customize my fish shell prompt?

Prompt Tab The "prompt" tab displays the contents of the current fish shell prompt. It allows selection from 17 predefined prompts. To change the prompt, select one and press "Prompt Set!".

Where is the fish config file?

The configuration file runs at every login and is located at ~/. config/fish/config. fish . Adding commands or functions to the file will execute/define them when opening a terminal, similar to .

How do you set a fish's variable?

Variables. Unlike other shells, fish has no dedicated VARIABLE=VALUE syntax for setting variables. Instead it has an ordinary command: set , which takes a variable name, and then its value.


1 Answers

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

like image 160
Dennis Avatar answered Sep 22 '22 07:09

Dennis