在 Mac 中为 Fish 重用 .bash_profile

joe*_*san 8 mac terminal bash iterm fish

我在我的 Mac 上使用 iTerm 并且我有一个我一直很舒服地使用的 .bash_profile。我最近了解了 fish bash 并将其安装在我的 Mac 上,突然间我的 .bash_profile 没有来源。关于为什么我看不到它的任何想法?

我如何指示我的 iTerm 和 fish 获取我的 .bach_profile ,就像以前没有鱼一样?

Kur*_*der 7

Fish 有一个用户控制的配置文件,默认命名为 $HOME/.config/fish/config.fish。Fish 还有一个与 bash/zsh/sh 兼容的导出命令,但它只是围绕着 fish 表单的一个薄包装:

set -gx VAR value
Run Code Online (Sandbox Code Playgroud)

至于 bash 别名,您有两种选择:将它们转换为缩写(参见“abbr”命令)或函数。在fish中,你可以用它的“别名”命令定义一个函数,但这只是

alias myalias some_command --arg1 --arg2
Run Code Online (Sandbox Code Playgroud)

进入

function myalias; some_command --arg1 --arg2 $argv; end
Run Code Online (Sandbox Code Playgroud)

正如格伦杰克曼所指出的“鱼不是bash”。它不是改进的 bash。改用鱼并不难,但确实需要一点努力。我在 13 个月前进行了转换,并认为值得付出努力。


Pav*_*eev 5

您可以通过overtrue使用此脚本:[gist link]

它基本上解析 .bash_profile 并在 Fish 中设置相同的环境变量。
对我来说效果很好!

# 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
Run Code Online (Sandbox Code Playgroud)

  • 上述脚本旨在与 Oh My Fish (https://github.com/oh-my-fish/oh-my-fish) 一起使用,方法是将其写入初始化文件`~/.config/omf/init。使用命令 `curl -o ~/.config/omf/init.fish https://gist.githubusercontent.com/overtrue/f7cd321708ba917b8def/raw/88d5930885210b9cee49782b4bc9bea8efd746ec/init.fish` (2认同)