1. 程式人生 > >linux[基礎]-10-命令行通配符[01]

linux[基礎]-10-命令行通配符[01]

pri /dev/ dir 字符 tor 依然 一段 cto dev

命令行通配符

命令行通配符:

一段例子:

[[email protected] ~]# ls /dev/sda

        /dev/sda

        [[email protected] ~]# ls /dev/sda1

        /dev/sda1

        [[email protected] ~]# ls /dev/sda2

        /dev/sda2

        [[email protected] ~]# ls /dev/sda3

        ls: cannot access /dev/sda3: No such file or directory

通配符的作用在這裏就是你不確定有多少個分區,分區號,他可以幫你確定

      * 匹配0個或多個字符

      ? 匹配任意單個個字符

      [0-9] 匹配範圍內的數字

      [abc] 匹配已給出的字符

實例:

[[email protected] ~]# ls /dev/sda*

        /dev/sda /dev/sda1 /dev/sda2

        [[email protected] ~]# ls /dev/sda?

        /dev/sda1 /dev/sda2

        [[email protected] ~]# ls /dev/sda[0-9]

        /dev/sda1 /dev/sda2

        [[email protected] ~]# ls /dev/sd[abc]

        /dev/sda

        [[email protected] ~]# ls /dev/sd[abc]?

        /dev/sda1 /dev/sda2

        [[email protected] ~]# ls /dev/sd[abc]*

        /dev/sda /dev/sda1 /dev/sda2

另外bash還支持許多特殊字符擴展:

\(反斜杠) 轉義後面單個字符

‘’(單引號) 轉義所有字符

“”(雙引號) 變量依然生效

``(反引號) 執行命令語句

變量生效:

[[email protected] ~]# Price=5

        [[email protected] ~]# echo "Price is $Price"

        Price is 5

但是想顯示價格為$5,它又和變量沖突,現在就該使用轉義字符 \ 反斜杠

[[email protected] ~]# echo "Price is $$Price"

        Price is 6892Price

        [[email protected] ~]# echo "Price is \$$Price"

        Price is $5

使用單引號表示轉義所有字符

[[email protected] ~]# echo ‘Price is \$$Price‘

        Price is \$$Price

使用``來執行命令語句

[[email protected] ~]# echo `uname -a`

        Linux Msl23-linux.com 3.10.0-123.el7.x86_64 #1

        SMP Mon May 5 11:16:57 EDT 2014 x86_64 x86_64 x86_64 GNU/Linux

        [[email protected] ~]# echo `uname -r`

        3.10.0-123.el7.x86_64

linux[基礎]-10-命令行通配符[01]