zsh和bash共用自定义配置

根据这个博客:https://medium.com/@rajsek/zsh-bash-startup-files-loading-order-bashrc-zshrc-etc-e30045652f2e,正规的做法应该是将共用的自定义配置写到~/.profile里,然后在~/.profile里根据终端的不同调用~/.bashrc~/.zshrc

~/.zshrc~/.bashrc都是特化的配置文件,而~/.profile是通用的配置文件。我认为应该是由特化的配置文件调用通用的配置文件,而不是现在这样反过来。因此我采用了这里的方案:https://stackoverflow.com/a/26020688,将通用配置文件写入~/.profile,然后在~/.bashrc~/.zshrc里source它。有些发行版里~/.profile调用了~/.bashrc,要把这个调用删掉,不然会导致互相调用造成死递归。

不过要注意每次启动bash都会读取~/.bashrc。为了避免重复export环境变量,我们可以让它只在login的时候source ~/.profile

# https://unix.stackexchange.com/a/26782
if shopt -q login_shell; then
	source ~/.profile
fi

有些发行版在用ssh登录时如果~/.bash_profile存在则会调用它而不是~/.profile。所以我们也可以在~/.bash_profile里:

[[ -f ~/.bashrc ]] && . ~/.bashrc
source ~/.profile

vscode

vscode的integrated terminal在启动的时候不会source ~/.profile。如果参照这里的方法:https://stackoverflow.com/a/67843008,把它变成一个login shell,那连接到remote上的时候code命令就没用了。

  "terminal.integrated.profiles.linux": {
    "bash": {
      "path": "bash",
      // login shell. Make it source ~/.profile
      "icon": "terminal-bash",
      "args": ["-l"]
    }
  },

但是vscode的integrated shell会设置一个环境变量VSCODE_SHELL_INTEGRATION=1,这个环境变量是不会继承到子进程的,所以我们可以在~/.bashrc里加上以下内容,使bash只在vscode integrated shell里source ~/.profile

if [ $VSCODE_SHELL_INTEGRATION ]; then
	. ~/.profile
fi
posted @ 2024-09-28 14:11  寻找繁星  阅读(5)  评论(0编辑  收藏  举报