I use the fish shell and would like to be able to "source" some shell scripts written with sh-compatible syntax, which fish cannot read. For example lots of software expects you to source some shell scripts to export helpful environment variables:
# customvars.sh
FOOBAR=qwerty
export FOOBAR
If I don't care about preserving local variable and function definitions, one workaround is to use exec to replace the current process with /bin/sh and then go back to fish
# We start out on fish
exec /bin/sh
# Running a POSIX shell now
. customvars.sh
exec /usr/bin/fish
# Back on fish
echo $FOOBAR
Is there a way to boil this pattern down to an one-liner that I can save in a function somewhere? If I try doing
exec /bin/sh -c '. vars.sh; /usr/bin/fish'
my terminal emulator window closes immediately instead of taking me back to an interactive fish prompt.
vars.sh
is not in your$PATH
. Tryexec /bin/sh -c '. ./vars.sh; exec fish'
to source the vars.sh in the current directory.bash
doesn't behave the POSIX way in that regard, except when in POSIX mode (called as "sh" or with --posix or with POSIXLY_CORRECT=1 or SHELLOPTS=posix in the environment). Instead, it looks for vars.sh in the current directory when not found in $PATH. In anycase, it's a bad idea to assume sh is bash..
is a special builtin, so causes the shell to exit upon failure. Usecommand .
instead of.
to remove that special attribute. If you want to source the file in the current directory, you have to use./
. Just like if you want to run thels
in the current directory, you have to use./ls
. That's just the way it is.