Skip to content
SRE运维进阶之路SRE运维进阶之路
github icon
  • 前端学习笔记

    第五章 引用

    author iconLinuxStorycalendar icon2021年5月11日category icon
    • Linux
    tag icon
    • Bash
    timer icon大约 2 分钟

    此页内容
    • 本章目录

    # 第五章 引用

    # 本章目录

    • 5.1 引用变量
    • 5.2 转义

    引用就是将一个字符串用引号括起来。这样做是为了保护Shell/Shell脚本中被重新解释过或带扩展功能的特殊字符open in new window(如果一个字符带有其特殊意义而不仅仅是字面量的话,这个字符就能称为“特殊字符”。比如星号“*”就能表示正则表达式open in new window中的一个通配符open in new window)。

    bash$ ls -l [Vv]*
    -rw-rw-r--    1 bozo  bozo       324 Apr  2 15:05 VIEWDATA.BAT
    -rw-rw-r--    1 bozo  bozo       507 May  4 14:25 vartrace.sh
    -rw-rw-r--    1 bozo  bozo       539 Apr 14 17:11 viewdata.sh
    
    bash$ ls -l '[Vv]*'
    ls: [Vv]*: No such file or directory
    
    1
    2
    3
    4
    5
    6
    7

    可以看到,提示不存在该文件。这里的'[Vv]*被当成了文件名。 在日常沟通和写作中,当我们引用一个短语的时候,我们会将它单独隔开并赋予它特殊的意义,而在bash脚本中,当我们引用一个字符串,意味着保留它的字面量。

    很多程序和公用代码会展开被引用字符串中的特殊字符。引用的一个重用用途是保护Shell中的命令行参数,但仍然允许调用的程序扩展它。

    bash$ grep '[Ff]irst' *.txt
    file1.txt:This is the first line of file1.txt.
    file2.txt:This is the First line of file2.txt.
    
    1
    2
    3

    在所有.txt文件中找出包含first或者First字符串的行

    注意,不加引号的 grep [Ff]irst *.txt 在Bash下也同样有效。[1]

    引用也可以控制echoopen in new window命令的断行符。

    bash$ echo $(ls -l)
    total 8 -rw-rw-r-- 1 bo bo 13 Aug 21 12:57 t.sh -rw-rw-r-- 1 bo bo 78 Aug 21 12:57 u.sh
    
    
    bash$ echo "$(ls -l)"
    total 8
     -rw-rw-r--  1 bo bo  13 Aug 21 12:57 t.sh
     -rw-rw-r--  1 bo bo  78 Aug 21 12:57 u.sh
    
    1
    2
    3
    4
    5
    6
    7
    8

    1. 前提是当前目录下有文件名为First或first的文件。这也是使用引用的另一个原因。(感谢 Harald Koenig 指出了这一点) ↩︎

    edit icon编辑此页open in new window
    上次编辑于: 2022/4/27 15:33:00
    贡献者: clay-wangzhi
    上一页
    第四章 变量与参数
    下一页
    第六章 退出与退出状态
    备案号:冀ICP备2021007336号
    Copyright © 2023 LinuxStory