Search code examples
linuxbashfish

Can fish, zsh and bash import the same configuration file?


I've recently been trying to switch from bash to zsh, or fish.

I have some alisa and PATH settings, but I don't want to manually copy them to zshrc or config.fish.

I tried writing them in a single file and using source ~/.myshrc to use them.

The alisa statement can be sourced normally. But when sourcing PATH in fish shell I got an error:

In fish, please use {$JAVA_HOME}. export
CLASSPATH=.:${JAVA_HOME}/lib:${JRE_HOME}/lib

I know that fish and bash syntax are incompatible.

So is there a general syntax that allows me to modify the PATH in myshrc and then all three shells can use it?

myshrc file like this:

# alias 
alias apts="apt search"
alias sf="aptitude search -F '%c %p %d %D %I'"
alias apti="sudo aptitude install"
alias aptup="sudo aptitude update"
alias aptgr="sudo aptitude upgrade"
alias aptpu="sudo aptitude purge"

# transset xterm
transset -t -a 0.95 >> /dev/null 2>&1


# set npm path
export PATH=~/.npm-global/bin:$PATH

# set java path
export JAVA_HOME=/usr/lib/jvm/java-17-openjdk-amd64 
export JRE_HOME=${java_home}/jre
export CLASSPATH=.:${JAVA_HOME}/lib:${JRE_HOME}/lib
export PATH=${JAVA_HOME}/bin:$PATH

# set android path
export ANDROID_HOME="/home/moly/Launcher/AndroidSDK/"
export PATH="${PATH}:${ANDROID_HOME}tools/:${ANDROID_HOME}platform-tools/"


Solution

  • One hacky way to use your existing configuration while trying to transition to a new shell like fish is simply to end your ~/.bashrc with fish, like so:

    # alias 
    alias apts="apt search"
    alias sf="aptitude search -F '%c %p %d %D %I'"
    alias apti="sudo aptitude install"
    alias aptup="sudo aptitude update"
    alias aptgr="sudo aptitude upgrade"
    alias aptpu="sudo aptitude purge"
    
    # transset xterm
    transset -t -a 0.95 >> /dev/null 2>&1
    
    
    # set npm path
    export PATH=~/.npm-global/bin:$PATH
    
    # set java path
    export JAVA_HOME=/usr/lib/jvm/java-17-openjdk-amd64 
    export JRE_HOME=${java_home}/jre
    export CLASSPATH=.:${JAVA_HOME}/lib:${JRE_HOME}/lib
    export PATH=${JAVA_HOME}/bin:$PATH
    
    # set android path
    export ANDROID_HOME="/home/moly/Launcher/AndroidSDK/"
    export PATH="${PATH}:${ANDROID_HOME}tools/:${ANDROID_HOME}platform-tools/"
    
    # start fish
    exec fish
    

    I don't think this is a very good solution, but this is the only one I know of to make fish inherit your bash environment without rewriting/translating your bashrc. While this is probably bad practice and could cause other sorts of issues, on the short term this could be what you need if you just want to try out fish in known territory with your aliases and PATH without spending time writing a configuration file perhaps for nothing.

    I have no experience with zsh and don't know if it can equally inherit the bash environment like that, but wouldn't be surprised if it can.