【Linux管理篇-2】Linux中的特殊符号

​此处所列仅为部分相对常用的特殊符号,不包括逻辑运算符、算术运算符、重定向符号等,并且与正则表达式相关的符号会在后续文章中继续更新,以另外的专题给出详细使用说明。

Linux中部分常用符号如下:

符号说明
#注释
;分号,连续指令执行
;;连续分号,专用在case分支语句的选项,担任Terminator的角色
.点号,表示当前目录
. .双点号,表示上一级目录
‘’单引号,表示一般字符串
“”双引号,表示一般字符串,允许变量扩展
``反引号,表示一个shell命令
,逗号,作为分隔符
/斜线,路径表示时,代表目录;单一的 / 代表 root 根目录的意思;在四则运算中,代表除法的符号。
\反斜线,转义,换行
|管道,上个指令的输出,做为下个指令的输入
!感叹号,表示非,不等于
?问号,匹配一个任意字符
*星号,匹配任意个字符
$name变量名称表示
$*所有的参数变量,是一个字符串
$@所有的参数变量,是一个列表
$#参数总个数
$?状态值

部分符号使用举例如下:

  1. 使用单个分号一次性执行多条命令

    ubuntu@localhost:~$ echo abc ; ls ; pwd
    abc
    Desktop    Downloads         Music     Public     Videos
    Documents  examples.desktop  Pictures  Templates  
    /home/ubuntu
    
  2. 在case语句中使用两个分号

    case  variable  in
    pattern1  |  pattern2 )  commands1;;
    pattern3 )  commands2;;
    *)  default  commands;;
    esac
    
  3. 点“.”表示当前目录

    ubuntu@localhost:~$ pwd
    /home/ubuntu
    ubuntu@localhost:~$ cd .
    ubuntu@localhost:~$ pwd
    /home/ubuntu
    
  4. 双点“. .”表示上一级目录

    ubuntu@localhost:~$ pwd
    /home/ubuntu
    ubuntu@localhost:~$ cd ..
    ubuntu@localhost:/home$ pwd
    /home
    
  5. 单引号表示一般字符串

    ubuntu@localhost:~$ echo 'hello linux'
    hello linux
    
  6. 双引号表示一般字符串

    ubuntu@localhost:~$ echo "hello linux abc"
    hello linux abc
    
  7. 单引号和双引号的区别:双引号对变量会进行引用,单引号直接输出

    ubuntu@localhost:~$ a=123
    ubuntu@localhost:~$ echo '$a'
    $a
    ubuntu@localhost:~$ echo "$a"
    123
    
  8. 反引号可以将一条命令作为参数

    ubuntu@localhost:~$ ls `locate string`
    /lib/xtables/libxt_string.so
    /usr/bin/strings
    /usr/bin/x86_64-linux-gnu-strings
    (省略部分输出)
    
  9. 管道符将多条命令的处理结果进行衔接,即上一条命令的输出作为下一条命令的输入直接使用

    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
    (省略部分输出)
    
  10. 问号匹配任意字符一次,该位置只有一个字符时可以被匹配

    ubuntu@localhost:~$ touch testfile1 testfile2 testfile345
    ubuntu@localhost:~$ ls testfile1 testfile2 testfile345 
    testfile1  testfile2  testfile345
    ubuntu@localhost:~$ ls testfile?
    testfile1  testfile2
    
  11. 星号匹配任意字符任意次,无论有几个字符都可以被匹配

    (上接10中的举例)
    ubuntu@localhost:~$ ls testfile*
    testfile1  testfile2  testfile345
    
  12. $name是变量名称表示

    ubuntu@localhost:~$ var1=123
    ubuntu@localhost:~$ echo $var1 
    123
    
  13. $*表示命令的所有参数列表,并且所有的参数组成一个字符串

    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
    
  14. $@表示命令的所有参数列表,并且所有的参数组成一个列表(数组)

    ubuntu@localhost:~$ vim test.sh
    #!/bin/bash
    echo $@
    ubuntu@localhost:~$ sh test.sh 123 def 1234 afndsfa 
    123 def 1234 afndsfa
    
  15. $#表示参数总个数

    ubuntu@localhost:~$ vim test.sh
    #!/bin/bash
    echo $#
    ubuntu@localhost:~$ sh test.sh 123 you abc 
    3
    
  16. $?为状态值,即查看上一条命令是否执行成功,若执行成功则返回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
    
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值