Shell 简介
什么是 Shell?
Shell 是用户与 Linux 内核之间的接口,它接收用户输入的命令,将其解释并传递给内核执行,然后将结果返回给用户。
┌──────────────────────────────────────────────────────────┐
│ 用户 │
│ │ │
│ ▼ │
│ ┌─────────────────────────────────────────────────┐ │
│ │ Shell │ │
│ │ • 命令解释器 │ │
│ │ • 脚本编程环境 │ │
│ │ • 用户交互界面 │ │
│ └─────────────────────────────────────────────────┘ │
│ │ │
│ ▼ │
│ ┌─────────────────────────────────────────────────┐ │
│ │ Linux 内核 │ │
│ │ • 进程管理 │ │
│ │ • 内存管理 │ │
│ │ • 文件系统 │ │
│ │ • 设备驱动 │ │
│ └─────────────────────────────────────────────────┘ │
│ │ │
│ ▼ │
│ 硬件 │
└──────────────────────────────────────────────────────────┘Shell 的功能
1. 命令解释
Shell 读取并执行用户输入的命令:
bash
$ ls -la /home
# Shell 解析这个命令,调用 ls 程序,传递参数 -la 和 /home2. 脚本编程
Shell 是一种脚本编程语言,可以编写自动化脚本:
bash
#!/bin/bash
for file in *.txt; do
echo "处理文件: $file"
done3. 环境管理
Shell 管理环境变量和工作环境:
bash
$ export PATH=$PATH:/new/path
$ echo $HOME4. 输入输出重定向
Shell 可以重定向命令的输入和输出:
bash
$ command > output.txt # 输出重定向
$ command < input.txt # 输入重定向
$ command1 | command2 # 管道5. 作业控制
Shell 可以管理后台进程:
bash
$ command & # 后台运行
$ jobs # 查看作业
$ fg # 恢复到前台常见的 Shell
Bash(Bourne Again Shell)
最流行的 Shell,是大多数 Linux 发行版的默认 Shell。
特点
- GNU 项目的一部分
- 兼容 Bourne Shell (sh)
- 丰富的功能:命令补全、历史、别名
- 强大的脚本能力
bash
# 检查是否使用 Bash
$ echo $BASH_VERSION
5.1.16(1)-releaseZsh(Z Shell)
功能更丰富的 Shell,macOS 的默认 Shell。
特点
- 强大的自动补全
- 主题和插件支持(Oh My Zsh)
- 拼写纠正
- 共享历史
bash
# 安装 Zsh
$ sudo apt install zsh
# 设置为默认 Shell
$ chsh -s /bin/zshFish(Friendly Interactive Shell)
用户友好的现代 Shell。
特点
- 开箱即用的语法高亮
- 智能自动补全
- 基于 Web 的配置
- 不兼容 POSIX
bash
# 安装 Fish
$ sudo apt install fish
# 运行 Fish
$ fish其他 Shell
| Shell | 说明 |
|---|---|
| sh (Bourne Shell) | 最早的 Unix Shell |
| csh (C Shell) | 类 C 语法 |
| tcsh | csh 的增强版 |
| ksh (Korn Shell) | 商业 Unix 常用 |
| dash | Debian 系统脚本默认 Shell |
查看和切换 Shell
查看当前 Shell
bash
# 方法 1:查看 $SHELL 变量
$ echo $SHELL
/bin/bash
# 方法 2:查看当前进程
$ ps -p $$
PID TTY TIME CMD
1234 pts/0 00:00:00 bash
# 方法 3:查看 $0
$ echo $0
-bash查看可用 Shell
bash
$ cat /etc/shells
/bin/sh
/bin/bash
/bin/zsh
/usr/bin/zsh
/bin/fish临时切换 Shell
bash
# 启动 Zsh
$ zsh
# 启动 Fish
$ fish
# 退出回到原 Shell
$ exit更改默认 Shell
bash
# 使用 chsh 命令
$ chsh -s /bin/zsh
# 或编辑 /etc/passwd(需要 root)
$ sudo usermod -s /bin/zsh usernameBash 配置文件
Bash 使用多个配置文件,在不同场景下加载。
配置文件概览
┌─────────────────────────────────────────────────────────────┐
│ Shell 启动类型 │
├─────────────────────────────────────────────────────────────┤
│ │
│ 登录 Shell 非登录 Shell │
│ (Login Shell) (Non-login Shell) │
│ │ │ │
│ ▼ ▼ │
│ /etc/profile /etc/bash.bashrc │
│ │ │ │
│ ▼ ▼ │
│ ~/.bash_profile ~/.bashrc │
│ 或 ~/.bash_login │
│ 或 ~/.profile │
│ │
└─────────────────────────────────────────────────────────────┘登录 Shell vs 非登录 Shell
登录 Shell
- 用户登录系统时启动(TTY 登录、SSH 登录)
- 执行
/etc/profile和~/.bash_profile
非登录 Shell
- 打开终端模拟器时启动
- 执行
/etc/bash.bashrc和~/.bashrc
bash
# 判断是否为登录 Shell
$ shopt -q login_shell && echo "Login Shell" || echo "Non-login Shell"
# 强制登录 Shell
$ bash --login
$ bash -l主要配置文件
/etc/profile(系统级,登录时)
bash
# 系统范围的环境变量设置
# 所有用户登录时执行~/.bash_profile(用户级,登录时)
bash
# 用户的登录配置
# 通常包含:
if [ -f ~/.bashrc ]; then
. ~/.bashrc
fi~/.bashrc(用户级,每次启动)
bash
# 用户的 Shell 配置
# 别名、函数、提示符等
# 别名
alias ll='ls -la'
alias grep='grep --color=auto'
# 环境变量
export EDITOR=vim
# 自定义函数
mkcd() {
mkdir -p "$1" && cd "$1"
}
# 提示符
PS1='\u@\h:\w\$ '~/.bash_logout(退出时)
bash
# 登出时执行的命令
# 例如清理临时文件加载配置文件
bash
# 重新加载 .bashrc
$ source ~/.bashrc
$ . ~/.bashrc
# 重新加载 .profile
$ source ~/.profileShell 提示符
PS1 - 主提示符
bash
# 查看当前 PS1
$ echo $PS1
\u@\h:\w\$
# 自定义提示符
$ PS1="[\t] \u@\h:\w\$ "
[10:30:00] maxwell@ubuntu:~$特殊字符
| 字符 | 含义 |
|---|---|
\u | 用户名 |
\h | 主机名 |
\H | 完整主机名 |
\w | 当前目录 |
\W | 当前目录名(不含路径) |
\d | 日期 |
\t | 时间(24小时) |
\T | 时间(12小时) |
\n | 换行 |
\$ | 提示符(普通用户 $,root #) |
\[...\] | 非打印字符包装(用于颜色) |
彩色提示符
bash
# 颜色代码
# \e[颜色代码m 开始,\e[0m 结束
# 示例:绿色用户名,蓝色路径
PS1='\[\e[32m\]\u@\h\[\e[0m\]:\[\e[34m\]\w\[\e[0m\]\$ '常用颜色代码
| 代码 | 颜色 |
|---|---|
| 30 | 黑色 |
| 31 | 红色 |
| 32 | 绿色 |
| 33 | 黄色 |
| 34 | 蓝色 |
| 35 | 紫色 |
| 36 | 青色 |
| 37 | 白色 |
PS2 - 续行提示符
bash
# 当命令未完成时显示
$ echo "hello
> world"PS3 和 PS4
bash
# PS3 - select 语句的提示符
# PS4 - 调试模式的提示符Bash 特性
命令历史
bash
# 查看历史
$ history
# 历史配置
HISTSIZE=1000 # 内存中保存的命令数
HISTFILESIZE=2000 # 历史文件中保存的命令数
HISTCONTROL=ignoreboth # 忽略重复和空格开头的命令
HISTIGNORE="ls:cd:pwd" # 忽略特定命令命令补全
bash
# Tab 补全文件和命令
$ cd Doc<Tab>
$ cd Documents/
# 安装增强补全
$ sudo apt install bash-completion别名
bash
# 定义别名
$ alias ll='ls -la'
# 查看别名
$ alias
# 删除别名
$ unalias ll
# 临时跳过别名
$ \ls
$ command ls命令类型
bash
# 内建命令:Shell 内置的命令
$ type cd
cd is a shell builtin
# 外部命令:独立的可执行文件
$ type ls
ls is /usr/bin/ls
# 别名
$ type ll
ll is aliased to 'ls -la'
# 函数
$ type mkcd
mkcd is a functionOh My Bash
Oh My Bash 是 Bash 的配置框架,提供主题和插件。
安装
bash
$ bash -c "$(curl -fsSL https://raw.githubusercontent.com/ohmybash/oh-my-bash/master/tools/install.sh)"配置
编辑 ~/.bashrc:
bash
# 主题
OSH_THEME="powerline"
# 插件
plugins=(git bashmarks)小结
本章介绍了 Shell 的基本概念:
- Shell 的作用:命令解释、脚本编程、环境管理
- 常见 Shell:Bash、Zsh、Fish
- 配置文件:
.bashrc、.bash_profile等 - 提示符定制:PS1 及颜色设置
- Bash 特性:历史、补全、别名
Shell 是 Linux 的核心工具,深入理解 Shell 将极大提高你的工作效率。接下来我们将学习输入输出重定向。
上一章:文件查找
下一章:输入输出重定向