針對 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

相關內容