此处所列仅为部分相对常用的特殊符号,不包括逻辑运算符、算术运算符、重定向符号等,并且与正则表达式相关的符号会在后续文章中继续更新,以另外的专题给出详细使用说明。
Linux中部分常用符号如下:
符号 | 说明 |
---|---|
# | 注释 |
; | 分号,连续指令执行 |
;; | 连续分号,专用在case分支语句的选项,担任Terminator的角色 |
. | 点号,表示当前目录 |
. . | 双点号,表示上一级目录 |
‘’ | 单引号,表示一般字符串 |
“” | 双引号,表示一般字符串,允许变量扩展 |
`` | 反引号,表示一个shell命令 |
, | 逗号,作为分隔符 |
/ | 斜线,路径表示时,代表目录;单一的 / 代表 root 根目录的意思;在四则运算中,代表除法的符号。 |
\ | 反斜线,转义,换行 |
| | 管道,上个指令的输出,做为下个指令的输入 |
! | 感叹号,表示非,不等于 |
? | 问号,匹配一个任意字符 |
* | 星号,匹配任意个字符 |
$name | 变量名称表示 |
$* | 所有的参数变量,是一个字符串 |
$@ | 所有的参数变量,是一个列表 |
$# | 参数总个数 |
$? | 状态值 |
部分符号使用举例如下:
-
使用单个分号一次性执行多条命令
ubuntu@localhost:~$ echo abc ; ls ; pwd abc Desktop Downloads Music Public Videos Documents examples.desktop Pictures Templates /home/ubuntu
-
在case语句中使用两个分号
case variable in pattern1 | pattern2 ) commands1;; pattern3 ) commands2;; *) default commands;; esac
-
点“.”表示当前目录
ubuntu@localhost:~$ pwd /home/ubuntu ubuntu@localhost:~$ cd . ubuntu@localhost:~$ pwd /home/ubuntu
-
双点“. .”表示上一级目录
ubuntu@localhost:~$ pwd /home/ubuntu ubuntu@localhost:~$ cd .. ubuntu@localhost:/home$ pwd /home
-
单引号表示一般字符串
ubuntu@localhost:~$ echo 'hello linux' hello linux
-
双引号表示一般字符串
ubuntu@localhost:~$ echo "hello linux abc" hello linux abc
-
单引号和双引号的区别:双引号对变量会进行引用,单引号直接输出
ubuntu@localhost:~$ a=123 ubuntu@localhost:~$ echo '$a' $a ubuntu@localhost:~$ echo "$a" 123
-
反引号可以将一条命令作为参数
ubuntu@localhost:~$ ls `locate string` /lib/xtables/libxt_string.so /usr/bin/strings /usr/bin/x86_64-linux-gnu-strings (省略部分输出)
-
管道符将多条命令的处理结果进行衔接,即上一条命令的输出作为下一条命令的输入直接使用
ubuntu@localhost:~$ cat /etc/login.defs |grep -v '^#' |grep -v ^$ MAIL_DIR /var/mail FAILLOG_ENAB yes LOG_UNKFAIL_ENAB no LOG_OK_LOGINS no SYSLOG_SU_ENAB yes SYSLOG_SG_ENAB yes (省略部分输出)
-
问号匹配任意字符一次,该位置只有一个字符时可以被匹配
ubuntu@localhost:~$ touch testfile1 testfile2 testfile345 ubuntu@localhost:~$ ls testfile1 testfile2 testfile345 testfile1 testfile2 testfile345 ubuntu@localhost:~$ ls testfile? testfile1 testfile2
-
星号匹配任意字符任意次,无论有几个字符都可以被匹配
(上接10中的举例) ubuntu@localhost:~$ ls testfile* testfile1 testfile2 testfile345
-
$name是变量名称表示
ubuntu@localhost:~$ var1=123 ubuntu@localhost:~$ echo $var1 123
-
$*表示命令的所有参数列表,并且所有的参数组成一个字符串
ubuntu@localhost:~$ vim test.sh #!/bin/bash echo $* ubuntu@localhost:~$ sh test.sh 123 abc 1 3 5 haha 123 abc 1 3 5 haha
-
$@表示命令的所有参数列表,并且所有的参数组成一个列表(数组)
ubuntu@localhost:~$ vim test.sh #!/bin/bash echo $@ ubuntu@localhost:~$ sh test.sh 123 def 1234 afndsfa 123 def 1234 afndsfa
-
$#表示参数总个数
ubuntu@localhost:~$ vim test.sh #!/bin/bash echo $# ubuntu@localhost:~$ sh test.sh 123 you abc 3
-
$?为状态值,即查看上一条命令是否执行成功,若执行成功则返回0
ubuntu@localhost:~$ ls file* file1 file2 file3 file4 file5 ubuntu@localhost:~$ echo $? 0 ubuntu@localhost:~$ lsnsdfna lsnsdfna: command not found ubuntu@localhost:~$ echo $? 127