Shell

基本命令

# 查看当前使用的是哪个 shell 程序
echo $SHELL

# $PATH 和 ${PATH} 一样

export、env、set

  • set 当前shell的变量,包括当前用户的变量
  • env 当前用户的变量
  • export 当前导出成用户变量的shell变量

其它

# . 和 . 是等价的
[ -s "$NVM_DIR/nvm.sh" ] && . "$NVM_DIR/nvm.sh" # This loads nvm
[ -s "$NVM_DIR/bash_completion" ] && . "$NVM_DIR/bash_completion"
nvm() { . "$NVM_DIR/nvm.sh" ; nvm $@ ; }

查看zhs的安装路劲:

which zsh

查看bash的安装路劲:

which bash

zsh切换bash

chsh -s /bin/bash

重新打开终端即可

bash切换zsh

chsh -s /bin/zsh

重新打开终端即可

iTerm2 显示 git 分支

将以下代码加入 /etc/profile 重启 iTerm2:

find_git_branch () {
  local dir=. head
  until [ "$dir" -ef / ]; do
    if [ -f "$dir/.git/HEAD" ]; then
      head=$(< "$dir/.git/HEAD")
      if [[ $head = ref: refs/heads/* ]]; then
        git_branch=" (${head#*/*/})"
      elif [[ $head != '' ]]; then
        git_branch=" → (detached)"
      else
        git_branch=" → (unknow)"
      fi
      return
    fi
    dir="../$dir"
  done
  git_branch=''
}

PROMPT_COMMAND="find_git_branch; $PROMPT_COMMAND"
black=$'[e[1;30m]'
red=$'[e[1;31m]'
green=$'[e[1;32m]'
yellow=$'[e[1;33m]'
blue=$'[e[1;34m]'
magenta=$'[e[1;35m]'
cyan=$'[e[1;36m]'
white=$'[e[1;37m]'
normal=$'[e[m]'
PS1="$white[$white@$greenh$white:$cyanW$yellow$git_branch$white]$ $normal"
更新时间:2025-03-14 15:50:46