bash 特殊字符

发布于 2025-05-14 20:34:00 字数 2997 浏览 3 评论 0

\ —— 表示转义

[root@shell ~]# echo $PATH
/usr/lib64/qt-3.3/bin:/usr/local/sbin:/usr/sbin:/sbin:/usr/local/bin:/usr/bin:/bin:/root/bin
[root@shell ~]# echo \$PATH   //打印普通的$字符
$PATH

& —— 后台运行

[root@shell ~]# sleep 100 &

; ​ —— 用来分隔多条命令(多条命令之间可以没有关系,即各个命令的结果互不影响)

[root@shell ~]# cd /tmp;touch douniwan
[root@shell tmp]# ls douniwan 
douniwan
[root@shell tmp]# cd /douniwan;touch suibian
bash: cd: /douniwan: No such file or directory
[root@shell tmp]# ls suibian 
suibian 

&& —— 连接两条命令,前面的命令执行成功,才执行后面的命令(并且)

[root@shell tmp]# ls douniwan && pwd
douniwan
/tmp
[root@shell tmp]# ls doushuiwan && pwd
ls: cannot access doushuiwan: No such file or directory    
[root@shell tmp]# make && make install    //后面会用到的,只有 make 成功了,才执行 make install

|| —— 连接两条命令,前面的命令执行失败,才会执行后面的命令(或者)

[root@shell tmp]# ls douniwan || pwd
douniwan
[root@shell tmp]# ls doushuiwan || pwd
ls: cannot access doushuiwan: No such file or directory
/tmp    

(cmds) —— 开启子 shell

在 shell 环境中再开启一个 shell,新开启的 shell 被称为子 shell,原来的 shell 被称为父 shell。

[root@shell script]# cd /tmp ; touch shadouxing      //在当前 shell 中执行的,会影响当前的 shell
[root@shell tmp]# pwd
/tmp
[root@shell tmp]# (cd /root ; touch shenmexing)    //在当前 shell 开启子 shell 去执行,命令执行结束,子 shell 终止,不会影响父 shell 的运行的。
[root@shell tmp]# pwd       //父 shell 的路径并未发生改变
/tmp
[root@shell tmp]# ls /root/shenmexing 
/root/shenmexing

子 shell 能够继承父 shell 中的环境变量

{cmds} —— 不开启子 shell,直接在当前 shell 中运行

引用:即屏蔽特殊符号的含义

'' —— 单引号,完全引用,也叫强引用。能够屏蔽除了自己以外的所有的特殊字符
"" —— 双引号,非完全引用,也叫弱引用。能够屏蔽大部分的特殊字符,屏蔽不了$和`

[root@shell tmp]# echo '$PATH'
$PATH
[root@shell tmp]# echo "$PATH"
/usr/lib64/qt-3.3/bin:/usr/local/sbin:/usr/sbin:/sbin:/usr/local/bin:/usr/bin:/bin:/root/bin

命令的引用(也叫命令的替换)

`` ​—— 反引号 使用的是命令的执行结果
$() —— 也是命令的引用

注意:默认的情况下,反引号是不支持嵌套的。

[root@shell tmp]# rpm -qf `which `
-enhanced-7.2.411-1.8.el6.x86_64
[root@shell tmp]# rpm -qf $(which )
-enhanced-7.2.411-1.8.el6.x86_64

嵌套:

[root@shell tmp]# dir_name=`basename `pwd``     // 嵌套会报错
basename: missing operand
Try `basename --help' for more information.
[root@shell tmp]# dir_name=$(basename $(pwd))   // 支持嵌套
[root@shell tmp]# echo $dir_name
tmp   

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

上一篇:

下一篇:

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。