
Estoy en Ubuntu 16.04, dado este test.file
contenido:
Hello \there
¿Por qué esto (desde la línea de comando):
sed 's#\\there#where#' test.file
funciona, pero esto:
sed "s#\\there#where#" test.file
¿no es? ¿Es cuestión de configuración?
El primero reemplaza con éxito el patrón, mientras que el segundo no parece encontrar ninguna coincidencia.
Necesito usar una variable dentro del texto de reemplazo en un script, así que (supongo que) necesito comillas dobles alrededor del comando sed.
Respuesta1
En bash
y otros shells, el carácter de barra invertida se maneja de manera diferente entre comillas simples o dobles.
Cuando escribe sed 's#\\there#where#' test.file
, lo que sed
ve en su cadena de ejecución es s#\\there#where# test.file
, porque las comillas simples impiden toda interpretación de caracteres especiales y secuencias de escape: incluso \'
no está permitido.
Cuando escribe sed "s#\\there#where#" test.file
, lo que sed
ve en su cadena de ejecución es s#\there#where# test.file
, porque las comillas dobles permiten algunas secuencias de escape, y el shell ha interpretado la primera barra invertida como un escape de la segunda.
La complicación adicional es que sed
también permite la interpretación de la secuencia de escape, similar a la de las comillas dobles, por lo que en el primer caso (comillas simples) la cadena de búsqueda se convierte en \there
, como desee; mientras que en el segundo caso (comillas dobles), el primer carácter de la cadena de búsqueda se convierte en Tab, seguido de here
.
El siguiente extracto del bash
manual define estas acciones:-
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.