Shell安装与配置 #

一、Shell安装 #

1.1 Linux系统 #

大多数Linux发行版默认已安装Bash。

检查Bash是否已安装:

bash
# 查看Bash路径
which bash

# 查看Bash版本
bash --version

# 查看当前Shell
echo $SHELL

安装Bash(如果未安装):

bash
# Debian/Ubuntu
sudo apt update
sudo apt install bash

# CentOS/RHEL
sudo yum install bash

# Fedora
sudo dnf install bash

# Arch Linux
sudo pacman -S bash

1.2 macOS系统 #

macOS默认使用zsh作为默认Shell,但也预装了Bash。

查看可用Shell:

bash
cat /etc/shells

切换默认Shell:

bash
# 切换到Bash
chsh -s /bin/bash

# 切换到Zsh
chsh -s /bin/zsh

安装最新版Bash(使用Homebrew):

bash
# 安装Homebrew(如果未安装)
/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"

# 安装最新版Bash
brew install bash

# 添加到可用Shell列表
sudo sh -c 'echo /usr/local/bin/bash >> /etc/shells'

# 切换到新版Bash
chsh -s /usr/local/bin/bash

1.3 Windows系统 #

Windows默认使用PowerShell或CMD,需要安装额外的工具来使用Bash。

方案一:WSL (Windows Subsystem for Linux)

powershell
# 启用WSL(以管理员身份运行PowerShell)
wsl --install

# 安装完成后重启电脑
# 默认安装Ubuntu,包含Bash

方案二:Git Bash

  1. 下载并安装 Git for Windows
  2. 安装完成后,Git Bash会自动安装
  3. 右键点击文件夹,选择"Git Bash Here"

方案三:Cygwin

  1. 下载 Cygwin
  2. 安装时选择需要的包(包括bash)
  3. 安装完成后运行Cygwin Terminal

二、Shell配置文件 #

2.1 配置文件类型 #

配置文件 说明 加载时机
/etc/profile 系统全局配置 登录Shell
/etc/bash.bashrc 系统全局配置 非登录Shell
~/.bash_profile 用户配置 登录Shell
~/.bash_login 用户配置(备用) 登录Shell
~/.profile 用户配置(备用) 登录Shell
~/.bashrc 用户配置 非登录Shell
~/.bash_logout 退出时执行 退出Shell

2.2 配置文件加载顺序 #

登录Shell加载顺序:

text
/etc/profile
    ↓
~/.bash_profile
    ↓
~/.bash_login
    ↓
~/.profile

非登录Shell加载顺序:

text
/etc/bash.bashrc
    ↓
~/.bashrc

2.3 推荐配置方式 #

~/.bash_profile 中加载 ~/.bashrc

bash
# 编辑 ~/.bash_profile
if [ -f ~/.bashrc ]; then
    source ~/.bashrc
fi

这样所有配置只需写在 ~/.bashrc 中即可。

三、常用配置项 #

3.1 环境变量配置 #

编辑 ~/.bashrc 文件:

bash
# 设置PATH
export PATH=$PATH:/usr/local/bin
export PATH=$PATH:$HOME/bin

# 设置编辑器
export EDITOR=vim

# 设置语言
export LANG=en_US.UTF-8

# 设置历史记录
export HISTSIZE=10000
export HISTFILESIZE=20000
export HISTCONTROL=ignoredups:erasedups

3.2 别名配置 #

bash
# 常用别名
alias ll='ls -alF'
alias la='ls -A'
alias l='ls -CF'
alias ..='cd ..'
alias ...='cd ../..'
alias grep='grep --color=auto'
alias fgrep='fgrep --color=auto'
alias egrep='egrep --color=auto'

# 安全别名
alias rm='rm -i'
alias cp='cp -i'
alias mv='mv -i'

# Git别名
alias gs='git status'
alias ga='git add'
alias gc='git commit'
alias gp='git push'
alias gl='git log --oneline'

3.3 函数定义 #

bash
# 创建目录并进入
mkcd() {
    mkdir -p "$1" && cd "$1"
}

# 快速备份
backup() {
    cp "$1" "$1.bak.$(date +%Y%m%d%H%M%S)"
}

# 查找文件
ff() {
    find . -name "*$1*" -type f
}

# 解压各种格式
extract() {
    if [ -f "$1" ]; then
        case "$1" in
            *.tar.bz2) tar xjf "$1" ;;
            *.tar.gz)  tar xzf "$1" ;;
            *.bz2)     bunzip2 "$1" ;;
            *.rar)     unrar x "$1" ;;
            *.gz)      gunzip "$1" ;;
            *.tar)     tar xf "$1" ;;
            *.tbz2)    tar xjf "$1" ;;
            *.tgz)     tar xzf "$1" ;;
            *.zip)     unzip "$1" ;;
            *.Z)       uncompress "$1" ;;
            *)         echo "不支持的格式: $1" ;;
        esac
    else
        echo "文件不存在: $1"
    fi
}

3.4 命令提示符配置 #

bash
# 简洁提示符
PS1='\u@\h:\w\$ '

# 彩色提示符
PS1='\[\033[01;32m\]\u@\h\[\033[00m\]:\[\033[01;34m\]\w\[\033[00m\]\$ '

# 显示Git分支
parse_git_branch() {
    git branch 2>/dev/null | sed -e '/^[^*]/d' -e 's/* \(.*\)/ (\1)/'
}
PS1='\u@\h:\[\033[01;34m\]\w\[\033[33m\]$(parse_git_branch)\[\033[00m\]\$ '

3.5 自动补全配置 #

bash
# 启用自动补全
if [ -f /etc/bash_completion ]; then
    . /etc/bash_completion
fi

# 大小写不敏感补全
bind 'set completion-ignore-case on'

# 自动显示补全列表
bind 'set show-all-if-ambiguous on'

四、使配置生效 #

4.1 重新加载配置 #

bash
# 方法一:使用source
source ~/.bashrc

# 方法二:使用点号
. ~/.bashrc

# 方法三:重新登录
exit
# 然后重新登录

4.2 验证配置 #

bash
# 查看环境变量
echo $PATH

# 查看别名
alias

# 查看所有环境变量
env

# 查看所有Shell变量
set

五、开发工具推荐 #

5.1 编辑器选择 #

编辑器 特点 推荐指数
Vim 终端编辑,功能强大 ★★★★★
VS Code 图形界面,插件丰富 ★★★★★
Nano 简单易用,适合新手 ★★★☆☆
Emacs 功能强大,学习曲线陡 ★★★★☆

5.2 VS Code配置 #

安装Shell相关插件:

  • Bash IDE:Bash语言支持
  • shellcheck:语法检查
  • ShellFormat:代码格式化
  • Bash Debug:调试支持

5.3 Vim配置 #

创建或编辑 ~/.vimrc

bash
" 语法高亮
syntax on

" 显示行号
set number

" 自动缩进
set autoindent
set shiftwidth=4
set tabstop=4

" 显示匹配括号
set showmatch

" 搜索高亮
set hlsearch
set incsearch

" Shell脚本特定设置
autocmd FileType sh setlocal expandtab shiftwidth=4 tabstop=4

六、Shell脚本开发规范 #

6.1 文件命名 #

bash
# 推荐命名方式
backup.sh          # 小写字母,下划线分隔
deploy_script.sh   # 描述性命名
check_disk.sh      # 动词开头

6.2 脚本头部 #

bash
#!/bin/bash
#
# 脚本名称:backup.sh
# 功能描述:自动备份指定目录
# 作    者:Your Name
# 创建日期:2026-03-27
# 版本号:v1.0.0
#

6.3 编码规范 #

bash
#!/bin/bash

# 设置严格模式
set -euo pipefail

# 定义常量
readonly SCRIPT_NAME=$(basename "$0")
readonly SCRIPT_DIR=$(cd "$(dirname "$0")" && pwd)

# 定义变量
backup_dir="/backup"
source_dir="/var/www"

# 定义函数
log() {
    echo "[$(date '+%Y-%m-%d %H:%M:%S')] $*"
}

# 主程序
main() {
    log "开始备份..."
    # 备份逻辑
    log "备份完成"
}

# 执行主程序
main "$@"

七、总结 #

7.1 配置要点 #

要点 说明
配置文件 ~/.bashrc 是主要配置文件
环境变量 使用 export 导出
别名 使用 alias 简化命令
函数 封装复杂操作
生效方式 source ~/.bashrc

7.2 下一步 #

环境配置完成后,让我们开始编写 第一个脚本,体验Shell脚本的魅力!

最后更新:2026-03-27