命令结构与语法 #
命令基本结构 #
标准格式 #
bash
command [options] [arguments]
┌─────────────────────────────────────────────────────────────┐
│ 命令结构解析 │
├─────────────────────────────────────────────────────────────┤
│ │
│ ls -la /home/user │
│ │ │ │ │
│ │ │ └── 参数(目标路径) │
│ │ └── 选项(修饰命令行为) │
│ └── 命令(要执行的程序) │
│ │
│ cp -r source/ destination/ │
│ │ │ │ │ │
│ │ │ │ └── 参数2(目标) │
│ │ │ └── 参数1(源) │
│ │ └── 选项(递归复制) │
│ └── 命令 │
│ │
└─────────────────────────────────────────────────────────────┘
命令组成部分 #
| 组成部分 | 说明 | 示例 |
|---|---|---|
| 命令 | 要执行的程序名称 | ls, cp, grep |
| 选项 | 修改命令行为的标志 | -l, -a, --help |
| 参数 | 命令操作的对象 | 文件名、目录名、字符串 |
选项类型 #
短选项 #
bash
# 短选项使用单个连字符
$ ls -l # 长格式显示
$ ls -a # 显示隐藏文件
$ ls -h # 人类可读格式
# 多个短选项可以合并
$ ls -la # 等同于 ls -l -a
$ ls -lah # 等同于 ls -l -a -h
# 带参数的短选项
$ tar -f archive.tar
$ grep -n "pattern" file.txt
$ head -n 10 file.txt
长选项 #
bash
# 长选项使用双连字符
$ ls --all # 等同于 ls -a
$ ls --human-readable # 等同于 ls -h
$ ls --size # 显示文件大小
# 长选项更易读
$ cp --recursive source/ dest/
$ rm --force --verbose file.txt
# 长选项的参数
$ grep --regexp="pattern" file.txt
$ head --lines=10 file.txt
$ tar --file=archive.tar
选项参数 #
bash
# 选项与参数之间的空格
$ head -n 10 file.txt # 有空格
$ head -n10 file.txt # 无空格(短选项)
$ head --lines=10 file.txt # 长选项用等号
# 常见带参数的选项
$ sort -k 2 file.txt # 按第 2 列排序
$ cut -d ',' -f 1 file.csv # 以逗号分隔,取第 1 列
$ find . -name "*.txt" # 查找 .txt 文件
$ grep -A 3 "pattern" file # 显示匹配行及后 3 行
参数类型 #
文件参数 #
bash
# 单个文件
$ cat file.txt
# 多个文件
$ cat file1.txt file2.txt file3.txt
# 使用通配符
$ cat *.txt
# 目录参数
$ ls /home/user/
$ cp -r source/ destination/
字符串参数 #
bash
# 带空格的字符串需要引号
$ echo "Hello World"
$ echo 'Hello World'
# 不带空格可以省略引号
$ echo Hello
# 变量作为参数
$ name="Alice"
$ echo "Hello, $name"
Hello, Alice
数字参数 #
bash
# 行数
$ head -n 10 file.txt
# 进程 ID
$ kill 1234
# 权限模式
$ chmod 755 script.sh
# 端口号
$ netstat -tlnp | grep 80
通配符 #
基本通配符 #
text
┌─────────────────────────────────────────────────────────────┐
│ 通配符说明 │
├─────────────────────────────────────────────────────────────┤
│ │
│ * 匹配任意数量的任意字符 │
│ ? 匹配单个任意字符 │
│ [...] 匹配方括号内的任意一个字符 │
│ [!...] 匹配不在方括号内的任意一个字符 │
│ │
└─────────────────────────────────────────────────────────────┘
星号 (*) #
bash
# 匹配所有 .txt 文件
$ ls *.txt
file1.txt file2.txt readme.txt
# 匹配所有以 file 开头的文件
$ ls file*
file1.txt file2.txt file_backup
# 匹配所有文件
$ ls *
# 注意:不包括隐藏文件(以 . 开头的文件)
# 匹配多级目录
$ ls */*.txt
dir1/file.txt dir2/file.txt
# 匹配任意层级目录(需要启用 globstar)
$ shopt -s globstar
$ ls **/*.py
问号 (?) #
bash
# 匹配单个字符
$ ls file?.txt
file1.txt file2.txt fileA.txt
# 匹配固定长度的文件名
$ ls ???.txt
abc.txt xyz.txt 123.txt
# 组合使用
$ ls file??.txt
file01.txt file02.txt fileAB.txt
方括号 […] #
bash
# 匹配指定字符
$ ls file[123].txt
file1.txt file2.txt file3.txt
# 匹配字符范围
$ ls file[a-z].txt
filea.txt fileb.txt ... filez.txt
$ ls file[0-9].txt
file0.txt file1.txt ... file9.txt
# 匹配多个范围
$ ls file[a-zA-Z0-9].txt
# 排除特定字符
$ ls file[!0-9].txt
filea.txt fileb.txt # 不包含数字
大括号扩展 {} #
bash
# 生成多个字符串
$ echo {a,b,c}
a b c
# 生成序列
$ echo {1..5}
1 2 3 4 5
$ echo {a..e}
a b c d e
# 组合使用
$ mkdir dir{1..3}
# 创建 dir1, dir2, dir3
$ touch file{1..3}.txt
# 创建 file1.txt, file2.txt, file3.txt
# 嵌套扩展
$ echo {a,b}{1,2}
a1 a2 b1 b2
# 步长
$ echo {0..10..2}
0 2 4 6 8 10
# 前缀和后缀
$ mv file.{txt,bak}
# 将 file.txt 重命名为 file.bak
引号与转义 #
引号类型 #
text
┌─────────────────────────────────────────────────────────────┐
│ 引号类型 │
├─────────────────────────────────────────────────────────────┤
│ │
│ 单引号 '...' 保留所有字符的字面值 │
│ 双引号 "..." 保留大多数字符,允许变量和命令替换 │
│ 反引号 `...` 命令替换(等同于 $(...)) │
│ │
└─────────────────────────────────────────────────────────────┘
单引号 #
bash
# 单引号内的所有字符都是字面值
$ echo 'Hello World'
Hello World
# 变量不被展开
$ name=Alice
$ echo 'Hello, $name'
Hello, $name
# 通配符不生效
$ echo '*.txt'
*.txt
# 特殊字符保留
$ echo 'It\'s a test'
# 注意:单引号内不能包含单引号,需要特殊处理
双引号 #
bash
# 变量会被展开
$ name=Alice
$ echo "Hello, $name"
Hello, Alice
# 命令替换
$ echo "Today is $(date +%Y-%m-%d)"
Today is 2026-04-11
# 通配符不展开
$ echo "*.txt"
*.txt
# 保留空格和换行
$ echo "Hello World"
Hello World
# 转义特殊字符
$ echo "Price: \$100"
Price: $100
反引号与 $() #
bash
# 命令替换(旧语法)
$ echo `date`
Fri Apr 11 10:00:00 CST 2026
# 命令替换(推荐语法)
$ echo $(date)
Fri Apr 11 10:00:00 CST 2026
# 嵌套命令替换
$ echo "Files: $(ls | wc -l)"
Files: 42
# 在变量中使用
$ current_dir=$(pwd)
$ echo $current_dir
/home/user
转义字符 #
bash
# 使用反斜杠转义
$ echo "Hello \"World\""
Hello "World"
$ echo Price: \$100
Price: $100
# 转义空格
$ ls My\ Documents/
# 查找名为 "My Documents" 的目录
# 转义换行(命令续行)
$ echo "This is a very \
> long line"
This is a very long line
# 转义通配符
$ echo \*
*
# 特殊转义字符
\n 换行
\t 制表符
\\ 反斜杠
\" 双引号
\' 单引号
特殊字符 #
Shell 元字符 #
text
┌─────────────────────────────────────────────────────────────┐
│ Shell 元字符 │
├─────────────────────────────────────────────────────────────┤
│ │
│ ; 命令分隔符 │
│ | 管道 │
│ & 后台执行 │
│ && 逻辑与 │
│ || 逻辑或 │
│ > 输出重定向 │
│ < 输入重定向 │
│ >> 追加重定向 │
│ $ 变量引用 │
│ \ 转义字符 │
│ ` 命令替换 │
│ ( ) 子 Shell │
│ { } 命令组 │
│ │
└─────────────────────────────────────────────────────────────┘
命令分隔符 #
bash
# 分号分隔多个命令
$ echo "First"; echo "Second"; echo "Third"
First
Second
Third
# 换行也可以分隔命令
$ echo "First"
$ echo "Second"
逻辑运算符 #
bash
# && 前一个命令成功才执行下一个
$ mkdir newdir && cd newdir
# 只有 mkdir 成功,才会执行 cd
$ test -f file.txt && echo "File exists"
# 文件存在才输出
# || 前一个命令失败才执行下一个
$ test -f file.txt || echo "File not found"
# 文件不存在才输出
$ cd /nonexistent || echo "Directory not found"
Directory not found
# 组合使用
$ test -f config.ini || cp default.ini config.ini
# 配置文件不存在则复制默认配置
后台执行 #
bash
# & 在后台执行命令
$ sleep 100 &
[1] 12345
# 查看后台任务
$ jobs
[1]+ Running sleep 100 &
# 将后台任务调到前台
$ fg %1
# 暂停前台任务
Ctrl + Z
# 将暂停任务放到后台
$ bg %1
命令组合 #
子 Shell #
bash
# ( ) 在子 Shell 中执行
$ (cd /tmp && ls)
# 子 Shell 中的变量不影响父 Shell
$ (x=100; echo $x)
100
$ echo $x
# 空,因为 x 是在子 Shell 中定义的
# 子 Shell 中的目录切换不影响父 Shell
$ pwd
/home/user
$ (cd /tmp; pwd)
/tmp
$ pwd
/home/user
命令组 #
bash
# { } 在当前 Shell 中执行
$ { cd /tmp; ls; }
# 注意:最后一个命令需要分号,} 前需要空格
$ { echo "First"; echo "Second"; }
# 命令组的输出重定向
$ { echo "Header"; cat data.txt; echo "Footer"; } > output.txt
# 与子 Shell 的区别
$ x=1
$ { x=100; }
$ echo $x
100 # 当前 Shell,变量被修改
命令替换详解 #
基本用法 #
bash
# 将命令输出赋值给变量
$ current_date=$(date +%Y-%m-%d)
$ echo $current_date
2026-04-11
# 在命令中使用
$ echo "There are $(ls | wc -l) files"
There are 42 files
# 嵌套使用
$ files=$(find . -name "*.txt" | wc -l)
$ echo "Found $files text files"
Found 15 text files
实际应用 #
bash
# 备份文件时使用时间戳
$ cp data.txt data_$(date +%Y%m%d).txt
# 获取脚本所在目录
$ script_dir=$(dirname "$0")
# 获取进程 ID
$ pid=$(pgrep nginx)
# 获取 IP 地址
$ ip=$(hostname -I | awk '{print $1}')
命令类型 #
内建命令 #
bash
# 内建命令是 Shell 自带的
$ type cd
cd is a shell builtin
$ type echo
echo is a shell builtin
$ type exit
exit is a shell builtin
# 常见内建命令
cd, pwd, echo, exit, export, source, alias, history
外部命令 #
bash
# 外部命令是独立的可执行文件
$ type ls
ls is /bin/ls
$ type grep
grep is /bin/grep
# 查看命令位置
$ which ls
/bin/ls
$ whereis grep
grep: /bin/grep /usr/share/man/man1/grep.1.gz
别名和函数 #
bash
# 查看命令类型
$ type ll
ll is aliased to `ls -alF'
$ type lsg
lsg is a function
lsg ()
{
ls "$1" | grep "$2"
}
命令优先级 #
text
┌─────────────────────────────────────────────────────────────┐
│ 命令查找顺序 │
├─────────────────────────────────────────────────────────────┤
│ │
│ 1. 别名 (alias) │
│ 2. 关键字 (keyword) │
│ 3. 函数 (function) │
│ 4. 内建命令 (builtin) │
│ 5. 外部命令 (外部程序) │
│ │
│ 使用 type 命令查看命令类型 │
│ 使用 command 命令跳过别名和函数 │
│ 使用 builtin 命令强制使用内建命令 │
│ │
└─────────────────────────────────────────────────────────────┘
获取帮助 #
man 手册 #
bash
# 查看命令手册
$ man ls
# 手册章节
1 用户命令
2 系统调用
3 库函数
4 特殊文件
5 文件格式
6 游戏
7 杂项
8 系统管理命令
# 指定章节
$ man 5 passwd # 查看 passwd 文件格式
$ man 1 passwd # 查看 passwd 命令
# 搜索手册
$ man -k "copy"
$ apropos copy
–help 选项 #
bash
# 大多数命令支持 --help
$ ls --help
$ grep --help
# 某些命令使用 -h
$ python -h
# 简短帮助
$ help cd # 内建命令使用 help
info 文档 #
bash
# 更详细的文档
$ info bash
$ info coreutils
# 导航
n 下一个节点
p 上一个节点
u 上级节点
q 退出
小结 #
通过本节学习,你应该掌握:
- 命令结构:命令、选项、参数的组合方式
- 选项类型:短选项和长选项的使用
- 通配符:*、?、[]、{} 的匹配规则
- 引号与转义:单引号、双引号、转义字符的区别
- 特殊字符:命令分隔符、逻辑运算符、后台执行
- 命令类型:内建命令、外部命令、别名、函数
- 获取帮助:man、–help、info 的使用
下一步,我们将学习文件的基础操作命令。
最后更新:2026-04-11