通配符是我们在shell环境中不知不觉中都会用到的,有时甚至都不会考虑到去探究其实现过程,因为使用得太普遍了。而清晰地理解每一个过程,将有助于我们的分析和调试。

说白了,通配符就是在 shell 环境下用来选择匹配给定模式的文件簇的特殊字符。下面是一段相对正式的定义,援引自https://bash.cyberciti.biz/guide/Wildcards

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 在参数中遇到了通配符时,会将其当做路径或文件名区在磁盘上搜寻最可能的匹配,若符合要求的匹配存在,则进行代换,否则就将该通配符作为一个普通字符传递给命令,然后由命令进行处理。

如下图1-1所示为 shell 进行命令行参数解析时的全过程,非常繁琐但很重要。

shell 常见通配符如表1所示。

而除了有通配符之外,还有一系列特有的特殊元字符,如表2所示。这些元字符基本是作用在命令上面,用作多命令分割或参数分割。要注意这里的  {}  ,因为表1里面也有类似的字符,实际上他们的作用范围并不相同,因此不会出现混淆。

metacharacter     A character that, when unquoted, separates words. One of the following: |  & ; ( ) < > space tab

另外对于表1和表2中的特殊字符,如果我们想让其变成普通字符,就需要使用转义符,shell中提供三种转义符,如表3所示。

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.

其中单引号与双引号的区别如下:

  • 单引号属于强引用,它会忽略所有被引起来的字符的特殊处理,被引用起来的字符会被原封不动地使用,只是引号范围内不允许再引用单引号。
  • 双引号属于弱引用,它会对其中几个被引用起来的字符做特殊处理,其它的则忽略。例如:
    1. 其中  $  加变量名可以取变量的值;
    2. 反引号和  $()  括号里面的字符会被当做命令执行后替换原来的字符;
    3. 需要使用  $ ` "  时需要通过反斜杠转义。

参考资料

[1] shell脚本【命令解析过程】

[2] Linux Shell 通配符、元字符、转义符使用实例介绍

Linux Bash之通配符的更多相关文章

  1. [转帖]linux bash环境变量简单总结

    linux bash环境变量简单总结 来源链接:http://www.178linux.com/8005 原创文章,如有转载,请注明原文地址 需要简单学习一下. 其实 我都是直接放一个 .sh文件到 ...

  2. 转: windows 10使用原生linux bash命令行

    转: https://www.zybuluo.com/pandait/note/337430 windows 10使用原生linux bash命令行 linux bash windows-10 第一时 ...

  3. Linux Bash代码 利用for循环实现命令的多次执行

    Linux Bash代码 [yuanhao15@lu01 libsvm-rank-2.81]$ for ((i=0; i<=19; i++)) do ./svm-train -s 5 -c 10 ...

  4. linux BASH shell设置字体与背景颜色

    linux BASH shell下设置字体及背景颜色的方法. BASH shell下设置字体及背景颜色  echo -e "\e[31mtest\e[41m"  \e[30m 将字 ...

  5. Linux:-bash: ***: command not found

    Linux:-bash: ***: command not found,系统很多命令都用不了,均提示没有此命令. 突然之间linux很多命令都用不了,均提示没有此命令. 这应该是系统环境变量出现了问题 ...

  6. Linux Bash命令关于程序调试详解

    转载:http://os.51cto.com/art/201006/207230.htm 参考:<Linux shell 脚本攻略>Page22-23 Linux bash程序在程序员的使 ...

  7. 禁用Linux bash rm --force

    防止无意的Linux bash rm --force 二.禁用rm -rf 因为rm -rf 删除文件的时候,经常会不小心将系统文件或者多个有用的目录删除掉.有两种方法:1,每次删除都用-i(inte ...

  8. linux中的通配符、元字符、转义符

    linux中的通配符.元字符.转义符 linux中的通配符元字符转义符 shell命令的构成 通配符 元字符meta 转义符 example reference shell命令的构成 每条linux命 ...

  9. linux bash关闭标准输出1(exec 1<&-)后重新打开

    linux bash shell的再次学习. 文件描述符: stdin,stdout 和 stderr 的文件描述符分别是 0,1 和 2(一个文件描述符说白了就是文件系统为了跟踪这个打开的文件而分配 ...

随机推荐

  1. 京东物流出问题了?褥了30块羊毛 & 浅析系统架构

    本人亲身经历,但后续的流程分析都是个人猜测的,毕竟没有实际做过这块的业务. 订单物流阻塞经过 火热的双11刚刚退去,截止今日,我在京东购买的矿泉水终于到货啦,下单两箱还只收到了一箱 :( ,从下单到收 ...

  2. 为什么坚果云Markdown值得使用?

    值得使用说明是被认为有价值的,值得有两种意思的解释:1.有价值,有意义:2.价钱合适,合算:那么坚果云Markdown是否是人们值得使用的呢?下面就来看看坚果云Markdown到底是什么?为什么值得使 ...

  3. 理解Java对象序列化【转】

      原文链接:http://www.blogjava.net/jiangshachina/archive/2012/02/13/369898.html 关于Java序列化的文章早已是汗牛充栋了,本文是 ...

  4. PHP安装扩展补充说明

    上一篇文章中用到了,php的sodium扩展,那么如何安装PHP扩展呢?基于我之前踩过的一些坑,大致整理了几种安装php扩展的方法.已安装sodium为例 1.先做点准备工作,安装sodium依赖 r ...

  5. 第二章 Unity Shader基础

    [TOC] 1. Unity Shader 的基础: ShaderLab 学习和编写着色器的过程一直是一个学习曲线很陡峭的过程,通常情况下为了自定义渲染效果往往要和很多文件和设置打交道,这些设置很容易 ...

  6. 理解Redis的单线程模式

    0.概述 本文基于的Redis版本为4.0以下,在Redis更高版本中并不是完全的单线程了,增加了BIO线程,本文主要讲述主工作线程的单线程模式. 通过本文将了解到以下内容: Redis服务器采用单线 ...

  7. ThinkPHP5——模型关联(一对一关联)

    定义 定义一对一关联使用了hasOne,hasOne方法的参数包括: hasOne('关联模型名','外键名','主键名',['模型别名定义'],'join类型'); 下面定义一个用户表,公司给每个用 ...

  8. pod install/update速度慢或失败的解决方案实践

    本文基于 https://www.cnblogs.com/dabaomo/p/9634727.html 声明 坚决拥护党的领导,本文章所用技术乃出于工作需要,敬请谅解. 正文 可以先过去快速浏览一遍再 ...

  9. 为什么QQ能帮你找到失散多年的兄弟?----图论

    编程三分钟的第 44 篇原创文章 为什么qq里"可能认识的人"功能推荐的如此精准? 为什么两个没有什么联系的朋友会相互认识? 一切的背后到底是道德的沦丧,还是人性的扭曲 ? 让我们 ...

  10. java 反射借助 asm 获取参数名称最优雅简单的方式

    背景说明 最近写反射相关的代码,想获取对应的参数名称,却发现没有特别好的方式. jdk7 及其以前,是无法通过反射获取参数名称的. jdk8 可以获取,但是要求指定 -parameter 启动参数,限 ...