Поведение ЭХА с и без двойных кавычек с шестнадцатеричным числом

Может кто-то помогать мне понять поведение echo? Я пробую следующие команды в Ubuntu:

$ echo -e \xaa
xaa
$ echo -e "\xaa"

▒
$

Как Вы видите с двойными кавычками, печатая hexadecimals, произведите, немного мусора. Я знаю -e может быть полезным для интерпретации \n к новой строке и другим последовательностям. Я просто хочу понять как эхо с -e опция обрабатывает hexadecimals.

2
задан 30 January 2016 в 06:32

1 ответ

Без кавычек, \x анализируется оболочкой для становления справедливым x:

$ printf "%s\n" echo -e \xaa
echo
-e
xaa    
$ printf "%s\n" echo -e "\xaa"
echo
-e
\xaa

Посмотрите man bash, раздел QUOTING:

   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).

Ваш grep вводит в заблуждение:

$ man echo | grep -o \xHH
xHH

grep -o печать точно те символы, которые соответствовали, обозначенный grep никогда не получал \.


Если Вы не работаете /bin/echo или env echo, встроенная оболочка echo будет выполнен. Так, если Вы хотите проверить документацию, работать help echo, или загляните man bash. man echo для /bin/echo:

$ echo --help
--help
$ env echo --help
Usage: echo [SHORT-OPTION]... [STRING]...
  or:  echo LONG-OPTION
Echo the STRING(s) to standard output.

  -n             do not output the trailing newline
  -e             enable interpretation of backslash escapes
  -E             disable interpretation of backslash escapes (default)
      --help     display this help and exit
      --version  output version information and exit

If -e is in effect, the following sequences are recognised:

  \\      backslash
...

Посмотрите man bash, раздел SHELL BUITLIN COMMANDS:

  echo interprets the following escape sequences:
  \a     alert (bell)
  \b     backspace
  \c     suppress further output
  \e
  \E     an escape character
  \f     form feed
  \n     new line
  \r     carriage return
  \t     horizontal tab
  \v     vertical tab
  \\     backslash
  \0nnn  the eight-bit character whose value is  the  octal  value
         nnn (zero to three octal digits)
  \xHH   the  eight-bit  character  whose value is the hexadecimal
         value HH (one or two hex digits)
2
ответ дан 2 December 2019 в 03:49

Другие вопросы по тегам:

Похожие вопросы: