bash脚本-格式

目标

  了解shell脚本中的特殊符号

特殊符号表

序号 名称 说明
1 # 表示单行注释
2 ~ 表示当前用户使用的家目录,可以直接在符号后加上某账户的名称。
3 ~+ 表示当前的工作目录
4 ; 表示连续指令
5 表示将字符转义为一般字符
6 表示将字符转义为一般字符,但允许变量拓展
7 `` 反引号
8 \ 反斜杠
9 | 管道符号
10 ! 感叹号
11 : 冒号
12 ? 问号

实操演示

  • 井号
[root@sinfotek tmp]# echo #sinfotek
  • ~ 波浪符
[root@sinfotek tmp]# echo ~
/root
[root@sinfotek tmp]# echo ~sinfotek
/home/sinfotek
  • ~+ 波浪加号
[root@sinfotek tmp]# echo ~+
/home/tmp
  • ; 分号
[root@sinfotek tmp]# echo 1 ; echo 2 ; echo 3 ;
1
2
3
  • 转移符号 ‘ “ /
[root@sinfotek tmp]# echo \# sinfotek #
# sinfotek
[root@sinfotek tmp]# echo # sinfotek #

[root@sinfotek tmp]# echo '# sinfotek #'
# sinfotek #
[root@sinfotek tmp]# echo '# sinfotek $(date)'
# sinfotek $(date)
[root@sinfotek tmp]# echo "# sinfotek $(date)"
# sinfotek 2022年 07月 15日 星期五 09:25:05 CST
  • | 管道符号
[root@sinfotek tmp]# cat shell  | grep sinfotek
name="sinfotek"
name="sinfotek"
  • ! 感叹号
[root@sinfotek tmp]# if [ $name != sinfotek ] ; then echo 1 $name ; else echo 2 $name ; fi
2 sinfotek
  • :冒号
[root@sinfotek tmp]# echo $(date) > time
[root@sinfotek tmp]# cat time 
20220715日 星期五 14:01:05 CST
[root@sinfotek tmp]# : > time 
[root@sinfotek tmp]# cat time 
  • ? 问号
[root@sinfotek tmp]# ls
shabc  shall  shasd  shcll  shell
[root@sinfotek tmp]# ls sh?ll
shall  shcll  shell
文档更新时间: 2022-07-15 14:06   作者:xiubao yan