How to source shell scripts in Fish

There may be a use case to source a general sh or bash script in fish. For example, if a function is present in a script file that needs to be sourced, fish cannot execute the script because sh functions are not natively supported.

How to source general scripts in fish

Sourcing general scripts in fish is trivial with a plugin named bass. Using a preferred package manager, bass can be installed and used to source scripts.

1
2
3
4
5
6
# variables.sh

example(){
  export VAR_NAME=test	
}
example
1
source ./variables.sh

Output:

1
2
from sourcing file ./variables.sh
source: Error while reading file “./variables.sh”

Using bass:

1
2
bass source ./variables.sh
echo $VAR_NAME

Output:

1
test