0

I want to prepend a couple directories to the $PATH variable temporarily so that I can set up a temporary development environment that I don't really want to change what other fish sessions see.

So I want to have a setup-env.fish script that prepends the proper paths to the beginning of $PATH so that they take precedence.

My simple test is

set -gx tool_dir "/home/jesse/sandbox/tools"
set PATH "$tool_dir/language/bin" $PATH
echo "Evnironment Set up"

I have made sure that those directories exist. When I open a shell and run that script, if I then echo $PATH that directory does not show up.

I understand that there is a way to persistently add a directory to set -U fish_user_paths path $fish_user_paths that then prepends to the global $PATH variable. But I don't want that. If I want to mess around with some different tools, I don't want to much up my system variables. I'd rather just prepend to $PATH and close the shell if I want. I've read through some of the fish documentation and some github issues about altering $PATH and I gather there are some variables that are treated differently by fish so I just wasn't sure if I am missing something else here.

If I run those commands in the shell it works, but I want my script to do more and I don't want to type in everything in it.

I am using fish 2.7.1 on XUbuntu 16.04.

1
  • You might want to flag a moderator to request moving this question to Stackoverflow where more fish experts hang out. Commented Mar 8, 2018 at 16:11

3 Answers 3

1

I think you can change the scope of the fish_user_paths in the current shell only: I believe when you exit this fish session, the universal value of that variable will be unaffected.

set -g fish_user_paths "$tool_dir/language/bin" $fish_user_paths
1
  • Thanks Glenn, that didn't work when I ran the script but my original script worked when I used source, which I wasn't doing before.
    – Jesse
    Commented Mar 8, 2018 at 17:02
1

Turns out I had to source the script instead of just running it and then it works the way I wanted.

0

Another alternative is to create a fish function. With fish shell -x is a temporary variable that passes to children -l is temporary and will not pass to children. If you'd like a function to make the switch. Just write a function using vim as follows:

funced -e vi name_of_function

function name_of_function
   set -x PATH /home/username/some_path  $PATH
end

funcsave name_of_function

Now when you want to enable the alternative path just type name_of_function

If you open another terminal that is not a child of this present terminal it will not use the temporary value unless you run the name_of_function again.

Alternatively you could create a file

#!/usr/bin/fish
set -x PATH /home/username/some_path $PATH

and do

source ~/file_name

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .