Run bash scripts in fish with bass plugin

When using OVH OpenStack, you may need to run the autogenerated openrc.sh bash script to load the environments variables:

#!/bin/bash

# To use an Openstack cloud you need to authenticate against keystone, which
# returns a **Token** and **Service Catalog**. The catalog contains the
# endpoint for all services the user/tenant has access to - including nova,
# glance, keystone, swift.
#
export OS_AUTH_URL=https://auth.cloud.ovh.net/v3/
export OS_IDENTITY_API_VERSION=3
...
...
# Don't leave a blank variable, unset it if it was empty
if [ -z "$OS_REGION_NAME" ]; then unset OS_REGION_NAME; fi

However, if using the fish shell, doing a > source openrc.sh will produce an error (Because the script is in bash syntax).

Similarly doing a > bash openrc.sh won’t produce the expected results, because the exported variables won’t be left in the shell after the script finishes.

A workaround for this is to use the bass plugin, that can be installed using fisher:

  1. Install fisher

    curl https://git.io/fisher --create-dirs -sLo ~/.config/fish/functions/fisher.fish
    
  2. Install bass with fisher

    > fisher add edc/bass
    created new fishfile in ~/.config/fish/fishfile
    fetching https://codeload.github.com/edc/bass/tar.gz/HEAD
    linking ~/.config/fish/functions/__bass.py
    linking ~/.config/fish/functions/bass.fish
    added 1 plugin in 0.85s
    
  3. Run the bash script using bass:

    > bass source ~/Downloads/openrc.sh
    

References

  1. bass (github.com)
  2. fisher (github.com)
updatedupdated2020-09-292020-09-29