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僅允許顏色控製字元通過,因此輸出可以包含彩色文本,但不能包含可能會弄亂輸出的其他格式字元。

相關內容