文章目录
echo
bash
echo - display a line of text
Echo the STRING(s) to standard output.
-n do not output the trailing newline
-e enable interpretation of backslash escapes
-E disable interpretation of backslash escapes (default)
bash
//输出字符串
linux@Ubuntu:~$ echo hello
hello
linux@Ubuntu:~$ echo 'hello'
hello
linux@Ubuntu:~$ echo "hello"
hello
//输出变量
linux@Ubuntu:~$ echo $PATH
/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/loca
l/games:/snap/bin:/snap/bin:/home/linux/gcc-
4.6.4/bin/:/opt/Qt/6.2.4/gcc_64/bin:/opt/Qt/Tools/QtCreator/bin/
linux@Ubuntu:~$ echo "$PATH"
/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/loca
l/games:/snap/bin:/snap/bin:/home/linux/gcc-
4.6.4/bin/:/opt/Qt/6.2.4/gcc_64/bin:/opt/Qt/Tools/QtCreator/bin/
//输出通配符
linux@Ubuntu:~$ echo $PATH *
/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/loca
l/games:/snap/bin:/snap/bin:/home/linux/gcc-
4.6.4/bin/:/opt/Qt/6.2.4/gcc_64/bin:/opt/Qt/Tools/QtCreator/bin/ gcc-4.6.4 linux-
3.14-fs4412 linux-3.14-ok-fs4412.tar.gz love.zip main.c Music my_sock_file
opencv-2.4.9 opencv-2.4.9.zip Pictures Public QT6 share snap Templates test
test.c u-boot-2013.01 u-boot-2013.01.tar.bz2 Videos
linux@Ubuntu:~$ echo "$PATH *"
/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/loca
l/games:/snap/bin:/snap/bin:/home/linux/gcc-
4.6.4/bin/:/opt/Qt/6.2.4/gcc_64/bin:/opt/Qt/Tools/QtCreator/bin/ *
//输出转义符
linux@Ubuntu:~$ echo 'hello\n'
hello\n
linux@Ubuntu:~$ echo "hello\n"
hello\n
linux@Ubuntu:~$ echo -e "hello\n"
hello
//输出多个空格
linux@Ubuntu:~$ echo 'helo world'
helo world +0
linux@Ubuntu:~$ echo "helo world"
helo world
| 场景 | 变量解析 | 通配符展开 | 转义符解析 | 空格压缩 | 典型用例 |
|---|---|---|---|---|---|
| 不加引号 | √ | √ | × | √ | 简单无空格或特殊字符的字符串 |
| 单引号 | × | × | √ | × | 需严格保留原格式的文本 |
| 双引号 | √ | × | √ | × | 需变量替换或部分转义的场景 |
重定向
- 0 :标准输入(stdin),默认从键盘读取
- 1 :标准输出(stdout),默认输出到终端屏幕
- 2 :标准错误(stderr),默认输出到终端屏幕
输出重定向
| 重定向符 | 含义 | 实例 |
|---|---|---|
| > file | 将file文件重定向为输出源,新建模式 | ls /usr > Lsoutput.txt,将ls /usr的执行结果,写到Lsoutput.txt文件中,若有同名文件将被删除 |
| >> file | 将file文件重定向为输出源,追加模式 | ls /usr >> Lsoutput.txt,将ls /usr的执行结果,追加到Lsoutput.txt文件已有内容后 |
| 2> file | 将file文件重定向为标准错误 | ls noexistingfile.txt 2> err.log,使用ls命令,查看一个不存在的文件时,将系统错误提示保存在err.log文件中 |
| &> file | 将file文件重定向为标准输出和标准错误 | ls filename.txt $> Lsoutput.txt 正确的信息和错误的信息都存储到了 Lsoutput.txt |
| 2>&1 | 将标准出错重定向到标准输出 | ls test > log 2>&1 标准输出和标准出错信息都输出到log文件 |
输入重定向
| 重定向符 | 含义 | 实例 |
|---|---|---|
| < file | 将file文件重定向为输入源 | wc < file1,将file1中的内容作 为输入传给wc命令 |
| << file | 表示从标准输入设备(键盘)中读入,直到遇到 分界符才停止(读入的数据不包括分界符),这里的分界符其实就是自定义的字符串 | cat << 9,当遇到输入9时,输 入结束 |
| 命令 < 文 件 1 > 文 件 2 | 将文件 1 作为命令的输入设备,该命令的执行结果输出到文件 2 中 | cat < file > log,file作为cat命 令的输出,把cat的输出结果, 输出到log文件中 |
wc
- 用于统计文件的行数、单词数、字节数或字符数
- 选项:
- -l 统计行数(Lines)
- -w 统计单词数(Words,以空格/换行符分隔)
- -c 统计字节数(Bytes)
- -m 统计字符数(Characters,与编码有关)
管道
|:将前一个命令的 stdout 作为下一个命令的 stdin<font style="color:rgb(51,51,51);">cat /etc/passwd | wc -l</font>
命令置换
- 将命令的输出结果作为另一个命令的参数或变量值
| 特性 | ** command** |
** $(command)** |
|---|---|---|
| 语法结构 | 使用反引号包裹命令,易与单引号混淆 | 使用 $() 结构,视觉区分更清晰 |
| 嵌套支持 | 需通过转义符 \ 实现多层嵌套 | 直接支持多级嵌套,无需转义 |
| 兼容性 | 所有类 Unix 系统均支持 | 部分旧版本 Shell(如 tcsh )不支持 |
| 可读性 | 复杂场景下易混淆(如引号混合使用) | 代码逻辑更直观,便于维护 |
| 特殊字符处理 | 需手动转义空格、 $ 等符号 | 自动处理特殊字符,减少错误风险 |
bash
result=`echo \`whoami\``
result=$(echo $(whoami))
历史命令
histoty命令用于查看和管理用户在终端中执行过的历史命令记录,默认记录存储于 ~/.bash_history 文件
| ** 选项** | 作用 |
|---|---|
| -c | history -c 清空内存中的历史记录 |
| -d N | history -d 100 删除编号为 100 的命令 |
| -a | history -a 将当前会话新增命令写入 ~/.bash_history(不 覆盖现有内容) |
| -w | history -w 保存当前所有记录到 ~/.bash_history(覆盖原有内容) |
| -r | history -r 重新读取 ~/.bash_history 到内存 |
| -n | history 10 显示最近的 10 条命令 |
通配符
- 通配符是一种特殊语句,通配符是用来代替字符的
- 星号(*):匹配任意长度的字符串
- 用file_*.txt,匹配file_wang.txt、file_Lee.txt、file3_Liu.txt
- 问号(?):匹配一个长度的字符
- 用flie_?.txt,匹配file_1.txt、file1_2.txt、file_3.txt
- 方括号([...]):匹配其中指定的一个字符
- 用file_[otr].txt,匹配file_o.txt、file_r.txt和file_t.txt
- 方括号([-]):匹配指定的一个字符范围
- 用file_[a-z].txt,匹配file_a.txt、file_b.txt,直到file_z.txt
- 方括号([^...]):除了其中指定的字符,均可匹配
- 用file_[^otr].txt,除了file_o.txt、file_r.txt和file_t.txt的其他文件