less -r 和 less -R 之间的区别

less -r 和 less -R 之间的区别

less有两个选项:

-r或者--raw-control-chars

导致显示“原始”控制字符。

-R或者--RAW-CONTROL-CHARS

与 类似-r,但只有 ANSI“颜色”转义序列以“原始”形式输出。

这是否意味着当只有颜色控制字符时它们是等效的?我的输出^O在管道到时有less -R,但在管道到时没有less -r。这里发生了什么事?

答案1

根据手册页:

    -r or --raw-control-chars
          Causes "raw" control characters to be displayed.  The default is to display control characters using the
          caret  notation; for example, a control-A (octal 001) is displayed as "^A".  Warning: when the -r option
          is used, less cannot keep track of the actual appearance of the screen (since this depends  on  how  the
          screen  responds to each type of control character).  Thus, various display problems may result, such as
          long lines being split in the wrong place.

   -R or --RAW-CONTROL-CHARS
          Like -r, but only ANSI "color" escape sequences are output in "raw" form.  Unlike -r, the screen appear‐
          ance is maintained correctly in most cases.  ANSI "color" escape sequences are sequences of the form:

               ESC [ ... m

          where  the  "..."  is  zero  or  more color specification characters For the purpose of keeping track of
          screen appearance, ANSI color escape sequences are assumed to not move the cursor.  You  can  make  less
          think  that  characters  other  than  "m" can end ANSI color escape sequences by setting the environment
          variable LESSANSIENDCHARS to the list of characters which can end a color escape sequence.  And you  can
          make  less  think  that  characters other than the standard ones may appear between the ESC and the m by
          setting the environment variable LESSANSIMIDCHARS to the list of characters which can appear.

显然,less默认情况下会转义 ANSI 转义代码,并显示插入符 (^) 后跟代码。-r不会转义这些字符,因此如果输入包含随机二进制数据,控制台可能会由于任何 ANSI 控制字符而输出意外的乱码。 (这就是为什么less在不处理这些字符本身的情况下无法知道屏幕的外观。)-R仅允许颜色控制字符通过,因此输出可以包含彩色文本,但不能包含可能会弄乱输出的其他格式字符。

相关内容