单引号中的 sed 命令有效,但不使用双引号

wat*_*ery 7 linux sed command-line

鉴于此test.file内容,我在 Ubuntu 16.04 上:

Hello \there
Run Code Online (Sandbox Code Playgroud)

为什么会这样(从命令行):

sed 's#\\there#where#' test.file
Run Code Online (Sandbox Code Playgroud)

工作,但是这个:

sed "s#\\there#where#" test.file
Run Code Online (Sandbox Code Playgroud)

才不是?是配置的问题吗?

前者成功地替换了模式,而后者似乎没有找到任何匹配项。
我需要在脚本的替换文本中使用一个变量,所以我(我猜我)需要在 sed 命令周围加上双引号。

AFH*_*AFH 7

bash和其他 shell 中,反斜杠字符在单引号或双引号内的处理方式不同。

当您键入 时sed 's#\\there#where#' test.filesed在其运行字符串中看到的是s#\\there#where# test.file,因为单引号阻止所有特殊字符和转义序列解释:even\'是不允许的。

当您键入 时sed "s#\\there#where#" test.filesed在其运行字符串中看到的是s#\there#where# test.file,因为双引号允许一些转义序列,并且 shell 已将第一个反斜杠解释为转义第二个。

更复杂的是,sed它还允许转义序列解释,类似于双引号中的解释,因此在第一种情况下(单引号),搜索字符串变为\there,如您所愿;而在第二种情况下(双引号),搜索字符串的第一个字符变为 a Tab,后跟here.

bash手册中的以下摘录定义了这些操作:-

   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 sin?
   gle 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 spe?
   cial 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  preced?
   ing  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.

   The special parameters * and @ have special meaning when in double quotes (see PARAMETERS below).

   Words of the form $'string' are treated specially.  The word expands to string, with backslash-escaped charac?
   ters  replaced  as  specified  by the ANSI C standard.  Backslash escape sequences, if present, are decoded as
   follows:
          \a     alert (bell)
          \b     backspace
          \e
          \E     an escape character
          \f     form feed
          \n     new line
          \r     carriage return
          \t     horizontal tab
          \v     vertical tab
          \\     backslash
          \'     single quote
          \"     double quote
          \nnn   the eight-bit character whose value is the octal value nnn (one to three digits)
          \xHH   the eight-bit character whose value is the hexadecimal value HH (one or two hex digits)
          \uHHHH the Unicode (ISO/IEC 10646) character whose value is the hexadecimal value HHHH (one to four hex
                 digits)
          \UHHHHHHHH
                 the  Unicode  (ISO/IEC  10646)  character  whose value is the hexadecimal value HHHHHHHH (one to
                 eight hex digits)
          \cx    a control-x character

   The expanded result is single-quoted, as if the dollar sign had not been present.

   A double-quoted string preceded by a dollar sign ($"string") will cause the string to be translated  according
   to  the  current  locale.   If the current locale is C or POSIX, the dollar sign is ignored.  If the string is
   translated and replaced, the replacement is double-quoted.
Run Code Online (Sandbox Code Playgroud)