30

(I'm talking about the shell Fish, esp. Fish's Fish.)

For Bash/ZSH, I had ~/.profile with some exports, aliases and other stuff.

I don't want to have a separate config for environment variables for Fish, I want to re-use my ~/.profile. How?

In The FAQ, it is stated that I can at least import those via /usr/local/share/fish/tools/import_bash_settings.py, however I don't really like running that for each Fish-instance.

22

You can use Bash to parse /etc/profile and ~/.profile, and then start fish.

  1. Create /usr/local/bin/fishlogin with contents

    #!/bin/bash -l
    exec -l fish "$@"
    
  2. Make it executable

    sudo chmod +x /usr/local/bin/fishlogin
    
  3. Add it to /etc/shells

    echo /usr/local/bin/fishlogin | sudo tee -a /etc/shells
    
  4. Set it as your default shell

    sudo usermod -s /usr/local/bin/fishlogin $USER
    
  • So elegant! Should be the accepted answer IMO – yonix Mar 22 '17 at 8:11
  • 2
    Just in case anyone is wondering, the mac equivalent of usermod -s /usr/local/bin/fishlogin $USER is chsh -s /usr/local/fishlogin $USER – gloriphobia May 10 '17 at 9:52
  • 1
    If you get chsh: /usr/local/bin/fishlogin: non-standard shell need to add it to /etc/shells – Ben Marten Jun 3 '17 at 22:06
  • 1
    To fully imitate launching fish directly, fish "$@" should be replaced with exec -l fish "$@". exec replaces the bash process with fish, while -l causes that argv[0] for fish is -fish, which signals that this is a login shell. – jhrmnn Feb 12 '18 at 10:28
  • 1
    @Sz. Well, nope. Fish does not support subshells in the first place. And even if it did, it would not do so by executing your login shell, so no Bash would be spawned then. – Noé Rubinstein Oct 3 '18 at 11:12
15

My current solution (see here for a maybe more recent version):

egrep "^export " ~/.profile | while read e
    set var (echo $e | sed -E "s/^export ([A-Z_]+)=(.*)\$/\1/")
    set value (echo $e | sed -E "s/^export ([A-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)

    set -xg $var $value
end
  • 3
    can you explain what this does? – max pleaner Jan 13 '17 at 18:00
  • @maxpleaner AFAICT it looks through .profile for export statements and executes them as fish sets. It's kinda hacky, but clever. – Jared Smith Oct 9 '18 at 13:40
8

For a much cleaner solution, you can use the foreign env plugin:

fenv source ~/.profile
  • 3
    This should be the accepted solution. You could elaborate (install omf) – Jules Randolph Apr 18 '18 at 20:36
3

I tried sourcing .profile on fish startup and it worked like a charm for me.

just do :

echo 'source ~/.profile;clear;' >  ~/.config/fish/config.fish

Restart terminal or iterm2, test an alias from .profile to test.

Note : Won't work with more complex .profile files that use syntax not available in fish - credit @erb

  • Worked for me too! Running MacOSX. – Alexar Dec 6 '15 at 3:33
  • Won't work with more complex .profile files that use syntax not available in fish. – erb Oct 6 '16 at 10:12
  • 1
    @erb I agree with you, I added the caveat in the answer. – Eswar Rajesh Pinapala Oct 6 '16 at 22:54
2

Install dash and add this line to your config.fish:

env -i HOME=$HOME dash -l -c 'export -p' | sed -e "/PATH/s/'//g;/PATH/s/:/ /g;s/=/ /;s/^export/set -x/" | source
1

You can't. fish's syntax is too different from Bourne shell (/bin/sh) syntax. This is the same reason you can't use .profile with other non-Bourne-derived shells, such as csh and tcsh.

  • I don't want to fully execute .profile. I just want to get all exports from there. One easy way would be to egrep "^export" which would be good enough already for me. Another, more correct solution would be this. Also, I e.g. could run this import_bash_settings.py script which probably does something similar. So, there are obviously many ways to do this. With my question here, I was wondering how others have solved this. – Albert Jul 11 '12 at 14:18
1

If your distribution uses PAM, you could set your environment variables in your ~/.pam_environment file.

1

You can start Fish from Bash. If you do that, Fish will inherit all environment variables (export FOO=bar) from Bash. At this point, Bash will have already read your .profile (or the like).

bash-3.2$ export TEST="test"
bash-3.2$ fish
cmey@MBP ~> echo $TEST
test
1

You can use bass, a plugin to execute bash commands in fish.

  1. Install bass.

    $ git clone https://github.com/edc/bass.git
    $ cd bass
    $ make install
    
  2. And then, just put this in your config.fish:

    bass source ~/.profile
    

Your Answer

By clicking "Post Your Answer", you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.