通配符是咱们在shell环境中不知不觉中都会用到的,有时甚至都不会考虑到去探究其实现过程,由于使用得太广泛了。而清晰地理解每个过程,将有助于咱们的分析和调试。html
说白了,通配符就是在 shell 环境下用来选择匹配给定模式的文件簇的特殊字符。下面是一段相对正式的定义,援引自https://bash.cyberciti.biz/guide/Wildcards。shell
Wildcards is one of the most important features of Bash shell. It allows you to select a group of files. For example you can select all C programming files in a GUI file manager with mouse. To select all C programming files in a Bash shell you use wildcards. In short wildcards are nothing but special characters that allows you to select a group of files that matches certain pattern.通配符通常只出如今命令的参数中,当 shell 在参数中遇到了通配符时,会将其当作路径或文件名区在磁盘上搜寻最可能的匹配,若符合要求的匹配存在,则进行代换,不然就将该通配符做为一个普通字符传递给命令,而后由命令进行处理。bash
以下图1-1所示为 shell 进行命令行参数解析时的全过程,很是繁琐但很重要。app
shell 常见通配符如表1所示。less
而除了有通配符以外,还有一系列特有的特殊元字符,如表2所示。这些元字符基本是做用在命令上面,用做多命令分割或参数分割。要注意这里的 {} ,由于表1里面也有相似的字符,实际上他们的做用范围并不相同,所以不会出现混淆。ide
metacharacter A character that, when unquoted, separates words. One of the following: | & ; ( ) < > space tab另外对于表1和表2中的特殊字符,若是咱们想让其变成普通字符,就须要使用转义符,shell中提供三种转义符,如表3所示。ui
There are three quoting mechanisms: the escape character, single quotes, and double quotes. A non-quoted backslash (\) is the escape character. It preserves the literal value of the next character that follows, with the exception of <newline>. If a \<newline>
pair appears, and the backslash is not itself quoted, the \<newline> is treated as a line continuation (that is, it is removed from the input stream and effectively ignored).Enclosing characters in single quotes preserves the literal value of each character within the quotes. A single quote may not occur between single quotes, even when preceded by a backslash. Enclosing characters in double quotes preserves the literal value of all characters within the quotes, with the exception of $, `, \, and, when history expansion is enabled, !.
The characters $ and ` retain their special meaning within double quotes. The backslash retains its special meaning only when followed by one of the following characters: $, `, ", \, or <newline>. A double quote may be quoted within double quotes by preceding it with a backslash. If enabled, history expansion will be performed unless an ! appearing in double quotes
is escaped using a backslash. The backslash preceding the ! is not removed.其中单引号与双引号的区别以下:spa
- 单引号属于强引用,它会忽略全部被引发来的字符的特殊处理,被引用起来的字符会被原封不动地使用,只是引号范围内不容许再引用单引号。
- 双引号属于弱引用,它会对其中几个被引用起来的字符作特殊处理,其它的则忽略。例如:
- 其中 $ 加变量名能够取变量的值;
- 反引号和 $() 括号里面的字符会被当作命令执行后替换原来的字符;
- 须要使用 $ ` " 时须要经过反斜杠转义。
参考资料.net
[1] shell脚本【命令解析过程】命令行