
私は Ubuntu 16.04 を使用していますが、次のtest.file
内容が示されています:
Hello \there
なぜこうなるのでしょうか (コマンドラインから):
sed 's#\\there#where#' test.file
動作しますが、これは:
sed "s#\\there#where#" test.file
そうではありませんか? それは設定の問題ですか?
前者はパターンを正常に置き換えますが、後者は一致するものが見つからないようです。
スクリプト内の置換テキスト内で変数を使用する必要があるため、sed コマンドを二重引用符で囲む必要があります (そうだと思います)。
答え1
およびその他のシェルではbash
、バックスラッシュ文字は、一重引用符または二重引用符内では異なる方法で処理されます。
と入力するとsed 's#\\there#where#' test.file
、sed
その実行文字列に表示されるのは ですs#\\there#where# test.file
。これは、一重引用符によってすべての特殊文字とエスケープ シーケンスの解釈が防止されるためです。 も\'
許可されません。
と入力するとsed "s#\\there#where#" test.file
、sed
実行文字列に表示されるのは ですs#\there#where# test.file
。これは、二重引用符によって一部のエスケープ シーケンスが許可され、シェルが最初のバックスラッシュを 2 番目のバックスラッシュをエスケープするものと解釈したためです。
さらに複雑なのはsed
、二重引用符の場合と同様に、エスケープ シーケンスの解釈も許可されることです。そのため、最初のケース (一重引用符) では、検索文字列は\there
希望どおり になりますが、2 番目のケース (二重引用符) では、検索文字列の最初の文字が になり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.