Reuse .bash_profile for Fish in Mac

15,616

Solution 1

Fish has exactly one user controlled config file which is named $HOME/.config/fish/config.fish by default. Fish also has an export command for compatibility with bash/zsh/sh but it just a thin wrapper around the fish form:

set -gx VAR value

As for bash aliases you have two choices: turn them into abbreviations (see the "abbr" command) or functions. In fish you can define a function with its "alias" command but that simply turns

alias myalias some_command --arg1 --arg2

into

function myalias; some_command --arg1 --arg2 $argv; end

As Glenn Jackman pointed "fish is not bash". It is not an improved bash. Switching to fish isn't hard but does require a little effort. I made the switch 13 months ago and think it is worth the effort.

Solution 2

You can use this script by overtrue: [gist link]

It basically parses .bash_profile and sets the same environment variables in Fish.
Works great for me!

# Fish shell

egrep "^export " ~/.bash_profile | while read e
    set var (echo $e | sed -E "s/^export ([A-Za-z_]+)=(.*)\$/\1/")
    set value (echo $e | sed -E "s/^export ([A-Za-z_]+)=(.*)\$/\2/")

    # remove surrounding quotes if existing
    set value (echo $value | sed -E "s/^\"(.*)\"\$/\1/")

    if test $var = "PATH"
        # replace ":" by spaces. this is how PATH looks for Fish
        set value (echo $value | sed -E "s/:/ /g")

        # use eval because we need to expand the value
        eval set -xg $var $value

        continue
    end

    # evaluate variables. we can use eval because we most likely just used "$var"
    set value (eval echo $value)

    #echo "set -xg '$var' '$value' (via '$e')"
    set -xg $var $value
end

Solution 3

I am using similar one to Pavel's script, but I am also reusing aliases.

Place this into ~/.config/fish/config.fish and then restart your terminal.

# REUSE ALIASES FROM ~/.bash_profile
egrep "^alias " ~/.bash_profile | while read e
        set var (echo $e | sed -E "s/^alias ([A-Za-z0-9_-]+)=(.*)\$/\1/")
        set value (echo $e | sed -E "s/^alias ([A-Za-z0-9_-]+)=(.*)\$/\2/")

        # remove surrounding quotes if existing
        set value (echo $value | sed -E "s/^\"(.*)\"\$/\1/")

    # evaluate variables. we can use eval because we most likely just used "$var"
        set value (eval echo $value)

    # set an alias
    alias $var="$value"
end

# REUSE ENVIRONMENT VARIABLES FROM ~/.bash_profile
egrep "^export " ~/.bash_profile | while read e
    set var (echo $e | sed -E "s/^export ([A-Z0-9_]+)=(.*)\$/\1/")
    set value (echo $e | sed -E "s/^export ([A-Z0-9_]+)=(.*)\$/\2/")

    # remove surrounding quotes if existing
    set value (echo $value | sed -E "s/^\"(.*)\"\$/\1/")

    if test $var = "PATH"
        # replace ":" by spaces. this is how PATH looks for Fish
        set value (echo $value | sed -E "s/:/ /g")

        # use eval because we need to expand the value
        eval set -xg $var $value

        continue
    end

    # evaluate variables. we can use eval because we most likely just used "$var"
    set value (eval echo $value)

    #echo "set -xg '$var' '$value' (via '$e')"

    switch $value
            case '`*`';
            # executable
            set NO_QUOTES (echo $value | sed -E "s/^\`(.*)\`\$/\1/")
            set -x $var (eval $NO_QUOTES)
        case '*'
            # default
            set -xg $var $value
        end
end
Share:
15,616

Related videos on Youtube

joesan
Author by

joesan

Updated on September 18, 2022

Comments

  • joesan
    joesan almost 2 years

    I'm using iTerm on my Mac and I have a .bash_profile that I have been comfortably using. I recently got to know about fish bash and I installed it on my Mac and all of a sudden my .bash_profile is not being sourced. Any ideas as to why I could not see it?

    How could I instruct my iTerm and fish to source my .bach_profile like it was doing before without fish?

    • DavidPostill
      DavidPostill over 7 years
      fish uses ~/.config/fish/config.fish for configuration.
    • glenn jackman
      glenn jackman over 7 years
      fish is not bash. It's a different language with a different syntax. If there are functions or aliases you want to keep, you'll need to rewrite them. Be sure to read the tutorial
    • joesan
      joesan over 7 years
      Could you post me some examples? All I have in my .bash_profile are just some exports and some aliases. I would like to reuse them for fish!
    • Daniel Centore
      Daniel Centore over 6 years
      Possible duplicate of re-use '~/.profile` for Fish?
  • wfbarksdale
    wfbarksdale over 6 years
    after doing this how do I do the equivalent of source ~/.bash_profile
  • leymannx
    leymannx over 5 years
    @wfbarksdale – source ~/.config/fish/config.fish.
  • Scott - Слава Україні
    Scott - Слава Україні over 5 years
    Please explain what this does, how it works, and how to use it.  Please do not respond in comments; edit your answer to make it clearer and more complete.
  • Adam Erickson
    Adam Erickson over 5 years
    The above script is designed to be used with Oh My Fish (github.com/oh-my-fish/oh-my-fish) by writing it to the initialization file ~/.config/omf/init.fish with the command curl -o ~/.config/omf/init.fish https://gist.githubusercontent.com/overtrue/f7cd321708ba917b‌​8def/raw/88d59308852‌​10b9cee49782b4bc9bea‌​8efd746ec/init.fish
  • Mike Whitis
    Mike Whitis almost 4 years
    Suggest changing the environment variable regex to allow lower case variables ex: [A-Za-z0-9] similar to the aliases to allow for cases like http_proxy.