为鱼重新使用“~/.profile”?

Alb*_*ert 43 .profile shell fish

(我说的是贝壳,尤其是鱼的鱼。)

对于 Bash/ZSH,我有~/.profile一些导出、别名和其他东西。

我不想为 Fish 的环境变量单独配置,我想重新使用我的~/.profile. 如何?

在常见问题解答中,据说我至少可以通过 导入它们/usr/local/share/fish/tools/import_bash_settings.py,但是我真的不喜欢为每个 Fish 实例运行它。

Noé*_*ein 36

您可以使用bash解析/etc/profile~/.profile,然后开始fish。

  1. /usr/local/bin/fishlogin用内容创作

     #!/bin/bash -l
     exec -l fish "$@"
    
    Run Code Online (Sandbox Code Playgroud)
  2. 使其可执行

     sudo chmod a+rx /usr/local/bin/fishlogin
    
    Run Code Online (Sandbox Code Playgroud)
  3. 通过运行fishlogin并检查您是否最终进入 Fish shell 来检查它是否有效。按 Control+D 退出 Fish shell。

  4. 添加到 /etc/shells

     echo /usr/local/bin/fishlogin | sudo tee -a /etc/shells
    
    Run Code Online (Sandbox Code Playgroud)
  5. 将其设置为您的默认外壳。

    在 Linux 下:

     sudo usermod -s /usr/local/bin/fishlogin $USER
    
    Run Code Online (Sandbox Code Playgroud)

    在 macOS 下:

     chsh -s /usr/local/fishlogin $USER
    
    Run Code Online (Sandbox Code Playgroud)

  • 以防万一有人想知道,`usermod -s /usr/local/bin/fishlogin $USER` 的 mac 等价物是 `chsh -s /usr/local/fishlogin $USER` (3认同)
  • 如果你得到 `chsh: /usr/local/bin/fishlogin: non-standard shell` 需要把它添加到 `/etc/shells` (2认同)
  • 为了完全模仿直接发射鱼,`fish "$@"` 应该替换为 `exec -l fish "$@"`。`exec` 用fish 替换了bash 进程,而`-l` 导致fish 的`argv[0]` 是`-fish`,这表明这是一个登录shell。 (2认同)

jgi*_*ich 25

对于更简洁的解决方案,您可以使用外部 env插件:

fenv source ~/.profile
Run Code Online (Sandbox Code Playgroud)

  • 这应该是公认的解决方案。你可以详细说明(安装omf) (7认同)

Alb*_*ert 15

我当前的解决方案(请参阅此处了解更新的版本):

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

  • 你能解释一下这是做什么的吗? (3认同)

小智 6

您可以使用bass插件,在fish 中执行 bash 命令。

  1. 安装低音

    $ git clone https://github.com/edc/bass.git
    $ cd bass
    $ make install
    
    Run Code Online (Sandbox Code Playgroud)
  2. 然后,只需将其放入您的config.fish

    bass source ~/.profile
    
    Run Code Online (Sandbox Code Playgroud)


Esw*_*ala 5

我尝试在 fish 启动时采购 .profile,它对我来说就像一个魅力。

做就是了 :

echo 'source ~/.profile;clear;' >  ~/.config/fish/config.fish
Run Code Online (Sandbox Code Playgroud)

重启终端或 iterm2,测试一个别名 from .profileto test。

注意:不适用于使用 fish 中不可用语法的更复杂的 .profile 文件 - 信用@erb