Bash your Fish

I love shells. I spend an inordinate amount of every working day in one. That one happens to be Bash, arguably the most ubiquitous CLI shell in the world. It’s not bad at all, although sometimes I think CLIs could be so much more than they are today. Some other people agree too, but that delectable future seems a little way off.

An existing shell that aims to provide a better interactive experience is the Friendly Interactive SHell — Fish. I’ve recently started to spend some time with this shell although there is a problem with it: my collection of Bash functions that I’ve accumulated over the years in .bashrc are rendered impotent in this environment. Sure, I could spend some time and redefine them all in Fish’s shellscript dialect; or I could just abandon trying something new in the name of efficiency and laziness and head back to Bash. That does seem sad, and fortunately there is a way to bridge the gap.

For other souls in a similar situation to me, here’s some script you can pop in your ~/.config/fish/config.fish (Fish’s equivalent to .bashrc)

function bash_env
    set -x BASH_ENV ~/.bash_functions
    bash -c "$argv"
end

for BASH_FUNCTION in (bash_env 'declare -F' | awk '{print $3}')
    eval "function $BASH_FUNCTION; bash_env $BASH_FUNCTION \$argv; end"
end

You’ll need to change the BASH_ENV setting on the second line to point to the file where you declare your Bash functions. I’ve moved my functions out of .bashrc and into a separate file that I source from .bashrc. This is because I also source /etc/bash_completion from .bashrc, and that is quite a time consuming process to do every time you want to run a simple Bash function from Fish.

Once that’s done, simply fire up Fish and type ‘functions’. You should see all of your old, dear Bash functions appear in the list, ready to aid you in your daily endeavours, just like times of old. Now, try the Fish!

Leave a comment

Your email address will not be published. Required fields are marked *