Mac OS Catalina 版从 bash 迁移到 zsh

Mac OS Catalina 版从 bash 迁移到 zsh

有些人可能知道,升级到 Mac OS Catalina 后,Apple 正在提示用户迁移到 zsh 作为默认 shell。

现在,每次打开 bash 时都会出现警告。可以通过将下面的行添加到 ~/.bash_profile 来禁用它(有兴趣的人可以参考)。

export BASH_SILENCE_DEPRECATION_WARNING=1

不过,我认为很多人(包括我)都想转向 zsh。

我当前的 ~/.bash_profile 如下所示:

# searches this directory for executables first
export PATH="/usr/local/bin:$PATH"

# jenv
export PATH="$HOME/.jenv/bin:$PATH"
eval "$(jenv init -)"

# rbenv
export PATH="$HOME/.rbenv/bin:$PATH"
eval "$(rbenv init -)"

# pyenv
export PATH="$HOME/.pyenv/bin:$PATH"
eval "$(pyenv init -)"

# nodenv
export PATH="$HOME/.nodenv/bin:$PATH"
eval "$(nodenv init -)"

# node-build-definitions
export NODE_BUILD_DEFINITIONS="/usr/local/opt/node-build-update-defs/share/node-build"

# bash auto-completion
if [ -f $(brew --prefix)/etc/bash_completion ]; then
  . $(brew --prefix)/etc/bash_completion    
fi

# git branch in prompt
parse_git_branch() {
    git branch 2> /dev/null | sed -e '/^[^*]/d' -e 's/* \(.*\)/ (\1)/'
}

# bash profile theme
export PS1="\[\e[1;37m\]parthnaik:\[\033[33;1m\]\w\[\033[m\]\$(parse_git_branch) \n$ "
export CLICOLOR=1
export LSCOLORS=GxFxCxDxBxegedabagaced

# firevault memory security
alias sleepsafe='sudo pmset -a destroyfvkeyonstandby 1 hibernatemode 25 standby 0 standbydelay 0'
alias sleepdefault='sudo pmset -a destroyfvkeyonstandby 0 hibernatemode 3 standby 1 standbydelay 10800'

# enable / disable captive portal
alias disablecaptiveportal='sudo defaults write /Library/Preferences/SystemConfiguration/com.apple.captive.control Active -bool false'
alias enablecaptiveportal='sudo defaults write /Library/Preferences/SystemConfiguration/com.apple.captive.control Active -bool true'

我希望有人比我更有知识,能够帮助我了解什么该放在哪里,因为网上似乎存在一些相互矛盾的信息。

到目前为止,根据我所读的内容,以下是我所看到的不同建议:

  1. 将 ~/.bash_profile 复制粘贴到 ~/.zshrc。
  2. 在~/.zshrc底部添加以下代码:
if [ -f ~/.bash_profile ]; then 
    . ~/.bash_profile;
fi
  1. 创建一个 ~/.aliases 文件和一个 ~/.paths 文件,然后将它们获取/导入到 ~/bash_profile 以及 ~/.zshrc 以保持向后兼容性。

除此之外,我还有一个.sh 脚本,每天通过以下命令自动运行:

sh script_name.sh

我是否应该像下面这样将其更改为使用 zsh?如果所有 .sh 脚本都带有 bash 和 zsh,情况应该如此。

zsh script_name.sh

虽然我知道上述任何一种在功能上都可以,但我仍在寻求迁移方面的建议和最佳实践。理想情况下,我希望我的主题、自动完成和 git 分支设置(如上面的 ~/.bash_profile 中所示)能够像现在一样工作。

对于主题,我知道还有一个名为“oh-my-zsh”的插件可用。建议安装这个吗?

感谢您的帮助!

答案1

我决定使用 zsh 的纯主题。我的脚本可以正常工作,它们现在只是通过 zsh 运行。这是我的 ~/.zshrc 文件的样子:

# PATHS AND ALIASES
# searches this directory for executables first
export PATH="/usr/local/bin:$PATH"

# jenv
export PATH="$HOME/.jenv/bin:$PATH"
eval "$(jenv init -)"

# rbenv
export PATH="$HOME/.rbenv/bin:$PATH"
eval "$(rbenv init -)"

# pyenv
export PATH="$HOME/.pyenv/bin:$PATH"
eval "$(pyenv init -)"

# nodenv
export PATH="$HOME/.nodenv/bin:$PATH"
eval "$(nodenv init -)"

# node-build-definitions
export NODE_BUILD_DEFINITIONS="/usr/local/opt/node-build-update-defs/share/node-build"

# firevault memory security
alias sleepsafe='sudo pmset -a destroyfvkeyonstandby 1 hibernatemode 25 standby 0 standbydelay 0'
alias sleepdefault='sudo pmset -a destroyfvkeyonstandby 0 hibernatemode 3 standby 1 standbydelay 10800'

# enable / disable captive portal
alias disablecaptiveportal='sudo defaults write /Library/Preferences/SystemConfiguration/com.apple.captive.control Active -bool false'
alias enablecaptiveportal='sudo defaults write /Library/Preferences/SystemConfiguration/com.apple.captive.control Active -bool true'

# CLI SETTINGS
# enable the default zsh completions
autoload -Uz compinit && compinit

# set colors for displaying directories and files when the ls command is used
export LSCOLORS='GxFxCxDxBxegedabagaced'
export CLICOLOR=1

# theme
fpath+=("$HOME/.zsh/pure")
autoload -U promptinit && promptinit
prompt pure

# change the path color
zstyle :prompt:pure:path color white

相关内容