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.shis not in your$PATH. Tryexec /bin/sh -c '. ./vars.sh; exec fish'to source the vars.sh in the current directory. – Stéphane Chazelas May 19 '15 at 15:18bashdoesn'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. – Stéphane Chazelas May 19 '15 at 15:30.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 thelsin the current directory, you have to use./ls. That's just the way it is. – Stéphane Chazelas May 19 '15 at 15:48