zsh和bash共用自定义配置

阅读量: searchstar 2022-04-23 13:32:29
Categories: Tags:

根据这个博客:https://medium.com/@rajsek/zsh-bash-startup-files-loading-order-bashrc-zshrc-etc-e30045652f2e,正规的做法应该是将共用的自定义配置写到~/.profile里,然后在~/.profile里根据终端的不同调用~/.bashrc~/.zshrc。但是~/.profile可能并不会在打开bash的时候执行,而可能只在登录(重启)的时候执行。让~/.profile中的更改生效,有几种方法:

重启

如果是公共服务器,那其实应该尽量避免重启的。

ssh --login -i

根据这个回答:https://superuser.com/a/564930,如果要在ssh登录时让~/.profile执行,好像需要这样:

ssh user@host bash --login -i

但是tab键自动补全就用不了了。

(推荐)在~/.zshrc~/.bashrcsource ~/.profile

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

讨论

其实CentOS里的~/.bash_profile在用ssh登录时也会被调用。所以对于CentOS,其实也可以在~/.bash_profilesource ~/.profile。但是为了一致,还是统一在~/.bashrcsource ~/.profile吧。