Is there a way to "source" a sh script from the fish shell?
I like to put many of my environment variable definitions in the .profile
script. If I am using a POSIX-compatible interactive shell like bash, I can use the source command to re-export the environment variables from .profile
when it gets updated, without needing to open a new terminal window. Is there a way to do the same when I am using fish
, which uses a different syntax for exporting variables? Can I tell fish to run a sh
subproccess and reexport any variables it defines?
When I try doing the obvious sh .profile
the environment variables apparently only get defined in the child sh
process and don't get updated in the parent fish
process.
edit: the shell code defining my variables is this one
CAML_LD_LIBRARY_PATH="/home/hugo/.opam/4.01.0/lib/stublibs"; export CAML_LD_LIBRARY_PATH;
PERL5LIB="/home/hugo/.opam/4.01.0/lib/perl5:"; export PERL5LIB;
OCAML_TOPLEVEL_PATH="/home/hugo/.opam/4.01.0/lib/toplevel"; export OCAML_TOPLEVEL_PATH;
MANPATH=":/home/hugo/.opam/4.01.0/man"; export MANPATH;
PATH="/home/hugo/.opam/4.01.0/bin:/home/hugo/bin:/usr/local/bin:/usr/bin:/bin:/usr/local/games:/usr/games"; export PATH;
Unfortunately, this code is automatically generated by another program, so the conversion to Fish must also be made using an automated process. I tried using the following sed script
sed 's/\(.*\)="\(.*\)".*/set -x \1 \'\2\';/'
which outputs the following Fish code
set -x CAML_LD_LIBRARY_PATH '/home/hugo/.opam/4.01.0/lib/stublibs';
set -x PERL5LIB '/home/hugo/.opam/4.01.0/lib/perl5:';
set -x OCAML_TOPLEVEL_PATH '/home/hugo/.opam/4.01.0/lib/toplevel';
set -x MANPATH ':/home/hugo/.opam/4.01.0/man';
set -x PATH '/home/hugo/.opam/4.01.0/bin:/home/hugo/bin:/usr/local/bin:/usr/bin:/bin:/usr/local/games:/usr/games';
Which I can run using eval and ()
substitution
eval (my-sed-script)
However, fish is complaining when I try to set PATH. I think it might be special-casing PATH and expecting an array instead of a single string.
set: Warning: path component /home/hugo/.opam/4.01.0/bin:/home/hugo/bin:/usr/local/bin:/usr/bin:/bin:/usr/local/games:/usr/games may not be valid in PATH.
set: No such file or directory
set: Did you mean 'set PATH $PATH /home/hugo/bin:/usr/local/bin:/usr/bin:/bin:/usr/local/games:/usr/games'?
Solution 1:
For sourcing a general sh
script in fish
, one option is the bass plugin, which can source a bash script's environment variables into fish.
If you have fisherman, you can install it with fisher install edc/bass
, then run bass source <command>
.
Solution 2:
Unlike bash, the fish PATH is an array of directories: http://fishshell.com/docs/current/index.html#variables
sed '/^PATH=/s/:/\' \'/g; s/\(.*\)="\(.*\)".*/set -x \1 \'\2\';/'