Skip to content

环境搭建

Linux 环境

检查当前 Shell

bash
# 查看当前 Shell
echo $SHELL

# 查看所有可用的 Shell
cat /etc/shells

# 查看 Bash 版本
bash --version

安装 Bash

Ubuntu/Debian

bash
# 更新包列表
sudo apt update

# 安装 Bash
sudo apt install bash

# 验证安装
bash --version

CentOS/RHEL

bash
# 安装 Bash
sudo yum install bash

# 验证安装
bash --version

Arch Linux

bash
# 安装 Bash
sudo pacman -S bash

# 验证安装
bash --version

配置 Shell

设置默认 Shell

bash
# 设置 Bash 为默认 Shell
chsh -s /bin/bash

# 重新登录或执行
exec bash

配置文件

bash
# 用户配置文件
~/.bashrc      # 交互式非登录 Shell
~/.bash_profile # 登录 Shell
~/.profile     # 通用配置

# 系统配置文件
/etc/bash.bashrc
/etc/profile

安装常用工具

bash
# Ubuntu/Debian
sudo apt install -y \
    git \
    vim \
    curl \
    wget \
    tree \
    htop \
    jq

# CentOS/RHEL
sudo yum install -y \
    git \
    vim \
    curl \
    wget \
    tree \
    htop \
    jq

macOS 环境

检查当前 Shell

bash
# 查看当前 Shell
echo $SHELL

# 查看所有可用的 Shell
cat /etc/shells

# 查看 Bash 版本
bash --version

安装 Homebrew

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

# 添加到 PATH
echo 'eval "$(/opt/homebrew/bin/brew shellenv)"' >> ~/.zprofile
eval "$(/opt/homebrew/bin/brew shellenv)"

安装 Bash

bash
# 安装最新版 Bash
brew install bash

# 添加到系统 Shell 列表
sudo bash -c 'echo /opt/homebrew/bin/bash >> /etc/shells'

# 设置为默认 Shell
chsh -s /opt/homebrew/bin/bash

安装常用工具

bash
# 安装常用工具
brew install \
    git \
    vim \
    curl \
    wget \
    tree \
    htop \
    jq \
    coreutils

配置 Shell

Zsh 配置(macOS 默认)

bash
# 编辑配置文件
vim ~/.zshrc

# 添加常用配置
export PATH="/usr/local/bin:$PATH"
export EDITOR=vim
alias ll='ls -la'
alias la='ls -A'
alias l='ls -CF'

Bash 配置

bash
# 编辑配置文件
vim ~/.bashrc

# 添加常用配置
export PATH="/usr/local/bin:$PATH"
export EDITOR=vim
alias ll='ls -la'
alias la='ls -A'
alias l='ls -CF'

# 重新加载配置
source ~/.bashrc

Windows 环境

使用 Git Bash

安装 Git for Windows

  1. 下载 Git for Windows

  2. 运行安装程序

    • 选择默认选项
    • 确保添加到 PATH
    • 选择 Git Bash 作为默认终端
  3. 验证安装

    bash
    # 打开 Git Bash
    # 查看版本
    bash --version
    git --version

使用 WSL (Windows Subsystem for Linux)

启用 WSL

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

# 重启计算机

安装 Linux 发行版

bash
# 安装 Ubuntu
wsl --install -d Ubuntu

# 查看已安装的发行版
wsl -l -v

使用 WSL

bash
# 打开 Ubuntu
# 更新系统
sudo apt update && sudo apt upgrade -y

# 安装 Bash 和常用工具
sudo apt install -y bash vim git curl wget tree htop jq

使用 Cygwin

安装 Cygwin

  1. 下载 Cygwin

  2. 运行安装程序

    • 选择安装目录
    • 选择下载源
    • 选择要安装的包:
      • bash
      • vim
      • git
      • curl
      • wget
      • tree
      • jq
  3. 添加到 PATH

    • 将 Cygwin bin 目录添加到系统 PATH

终端配置

推荐终端

Linux/macOS

  • iTerm2 (macOS)

    • 功能强大的终端
    • 支持分屏和标签
    • 丰富的主题
  • GNOME Terminal (Linux)

    • 默认终端
    • 简洁易用
    • 支持配置文件

Windows

  • Windows Terminal

    • 微软官方终端
    • 支持 WSL、Git Bash
    • 现代化界面
  • Git Bash

    • 轻量级终端
    • Bash 环境
    • Git 集成

终端配置示例

Windows Terminal 配置

json
{
    "profiles": {
        "defaults": {
            "fontSize": 12,
            "fontFace": "Cascadia Code",
            "colorScheme": "Campbell"
        },
        "list": [
            {
                "guid": "{0caa0dad-35be-5f56-a8ff-afceeeaa6101}",
                "name": "Git Bash",
                "commandline": "C:\\Program Files\\Git\\bin\\bash.exe",
                "icon": "C:\\Program Files\\Git\\mingw64\\share\\git\\git-for-windows.ico"
            },
            {
                "guid": "{2c4de342-38b7-51cf-b940-2309a097f518}",
                "name": "Ubuntu",
                "source": "Windows.Terminal.Wsl"
            }
        ]
    }
}

文本编辑器

Vim

bash
# 安装 Vim
# Ubuntu/Debian
sudo apt install vim

# macOS
brew install vim

# 配置 Vim
vim ~/.vimrc

Vim 基本配置

vim
" 显示行号
set number

" 语法高亮
syntax on

" 自动缩进
set autoindent
set smartindent

" 显示匹配的括号
set showmatch

" 搜索时忽略大小写
set ignorecase

" 高亮搜索结果
set hlsearch

" 启用鼠标
set mouse=a

" 设置编码
set encoding=utf-8

VS Code

安装 VS Code

推荐插件

  • ShellCheck - Shell 脚本静态分析
  • Shell-format - Shell 脚本格式化
  • Bash IDE - Bash 语言支持
  • Code Runner - 直接运行代码

VS Code 配置

json
{
    "files.associations": {
        "*.sh": "shellscript"
    },
    "editor.formatOnSave": true,
    "[shellscript]": {
        "editor.defaultFormatter": "foxundermoon.shell-format"
    }
}

验证环境

检查安装

bash
# 检查 Bash
bash --version

# 检查常用命令
which git
which vim
which curl
which wget

# 检查 Shell
echo $SHELL
$0

# 测试脚本
echo 'echo "Hello World"' > test.sh
bash test.sh
rm test.sh

创建测试脚本

bash
# 创建测试脚本
cat > test_env.sh << 'EOF'
#!/bin/bash

echo "=== 环境测试 ==="
echo "Shell: $SHELL"
echo "Bash 版本: $(bash --version | head -n 1)"
echo "用户: $USER"
echo "主目录: $HOME"
echo "当前目录: $(pwd)"
echo "日期: $(date)"

echo ""
echo "=== 命令检查 ==="
for cmd in git vim curl wget tree; do
    if command -v $cmd &> /dev/null; then
        echo "✓ $cmd: $(command -v $cmd)"
    else
        echo "✗ $cmd: 未安装"
    fi
done

echo ""
echo "环境配置完成!"
EOF

# 运行测试
bash test_env.sh

# 清理
rm test_env.sh

常见问题

Q1: 如何切换 Shell?

bash
# 临时切换
zsh
bash

# 永久切换
chsh -s /bin/zsh
chsh -s /bin/bash

Q2: 配置文件不生效?

bash
# 重新加载配置文件
source ~/.bashrc
source ~/.zshrc

# 或执行
. ~/.bashrc
. ~/.zshrc

Q3: Windows 下找不到命令?

bash
# 检查 PATH
echo $PATH

# 添加到 PATH
export PATH="/path/to/bin:$PATH"

# 永久添加到配置文件
echo 'export PATH="/path/to/bin:$PATH"' >> ~/.bashrc

总结

环境搭建是学习 Shell 脚本的第一步。选择适合你操作系统的环境,安装必要的工具,配置好编辑器,就可以开始学习 Shell 脚本了。

下一节我们将创建第一个 Shell 脚本 - Hello World。