작은 따옴표로 묶인 sed 명령은 작동하지만 큰 따옴표를 사용하지 않습니다.

작은 따옴표로 묶인 sed 명령은 작동하지만 큰 따옴표를 사용하지 않습니다.

나는 다음 test.file내용을 고려하여 Ubuntu 16.04를 사용하고 있습니다.

Hello \there

왜 이런 일이 일어나는가(명령줄에서):

sed 's#\\there#where#' test.file

작동하지만 이것은 :

sed "s#\\there#where#" test.file

하지 않습니다? 구성의 문제인가요?

전자는 성공적으로 패턴을 대체했지만 후자는 일치하는 항목을 찾지 못하는 것 같습니다.
스크립트의 대체 텍스트 내에서 변수를 사용해야 하므로 sed 명령 주위에 큰따옴표가 필요할 것 같습니다.

답변1

및 기타 쉘 에서 bash백슬래시 문자는 작은따옴표 또는 큰따옴표 내에서 다르게 처리됩니다.

를 입력할 때 실행 문자열에 표시되는 것은 sed 's#\\there#where#' test.file작은 따옴표가 모든 특수 문자 및 이스케이프 시퀀스 해석을 방지하기 때문입니다. 심지어는 허용되지 않습니다.seds#\\there#where# test.file\'

를 입력하면 실행 문자열에 가 표시됩니다. 큰따옴표 sed "s#\\there#where#" test.file는 일부 이스케이프 시퀀스를 허용하고 쉘은 첫 번째 백슬래시를 두 번째 백슬래시를 이스케이프하는 것으로 해석했기 때문입니다.seds#\there#where# test.file

더 복잡한 점은 sed큰따옴표와 유사한 이스케이프 시퀀스 해석을 허용하므로 첫 번째 경우(작은따옴표)의 경우 검색 문자열은 \there원하는 대로 가 됩니다. 두 번째 경우(큰따옴표로 묶음)의 경우 검색 문자열의 첫 번째 문자는 가 되고 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.

관련 정보