Bax is a POSIX shell execution wrapper for the fish shell. Use it to run bash utilities, replaying environment changes in fish without leaving the comfort of your session.
Install with Fundle:
fundle plugin 'hunter-richardson/fish-bax'
- fish 2.0+
You need to run a script in bash and want to preserve changes in the environment, e.g., modifications to the $PATH
, exported and unset variables, and so on. What do you do? Nuke the current session.
$ exec bash -c "$commands; exec fish"
This is not a rare pattern. Fork a POSIX shell, run your scripts there, inherit the environment in fish. And if you're content with that, you're all set, grab it and off you go. Any caveats? Unfortunately, yes.
For starters, there's no way to preserve the last command exit status in the new shell. You'll lose the entire state of your session; history may not sync up correctly if you have fish running in other terminal tabs, local variables are gone. Fish takes a little while to start up. Moreover, things fish is configured to do on startup like running configuration snippets or displaying a custom greeting, may not be appreciated. If jobs are running in the background, they'll be terminated too.
To solve this problem, we run your commands in bash, capture the environment changes and reproduce them in fish, so you don't have to exec
-away your session. Now you can have your cake and eat it too.
Use bax
followed by the bash commands you want to run.
$ bax export PYTHON=python2
That will set the environment variable PYTHON
in your session.
$ env | string match "PYTHON=*"
PYTHON=python2
Use double quotes (or single quotes to avoid variable substitution) to enclose multiple commands separated by a semicolon. Here's an example that downloads the latest Node.js release using creationix/nvm.
$ bax "source ~/.nvm/nvm.sh --no-use; nvm use latest"
Changing the current directory in a subshell leaves you back where you were when you exit. Bax switches directories instead. To move backward through the directory history use cd -
or prevd
as you usually would.
$ bax cd ~
$ pwd
/Users/jorgebucaran
Bax supports bash aliases too. While it's unrealistic to handle every possible way of defining an alias, typical cases like command shortcuts work out of the box.
$ bax alias g=git
$ g init
Initialized empty Git repository in ~/Code/fish-bax/.git/
Bax is not infallible. Interactive utilities, such as ssh-add
are not currently supported.