为什么在 MacOSX 中使用 -e 参数时 echo 不支持“\e”(转义)

为什么在 MacOSX 中使用 -e 参数时 echo 不支持“\e”(转义)

当我尝试通过内置命令使用 ANSI 转义序列打印出一些彩色文本时echo,似乎\e我提供的字符串中的转义序列被按字面解释,而不是按它应该表示的“转义”解释。这种情况只发生在 Snow Leopard 中——下面的示例在 Leopard 中按预期工作。

显然echo确实支持开关,因为在使用时-e它可以正确解释:\n

~ $ 
~ $ echo "\n"
\n
~ $ echo -e "\n"


~ $ 

但是当我尝试使用时\e,我得到了这个:

~ $ echo -e "\e[34mCOLORS"
\e[34mCOLORS
~ $ 

就像我说的,在 Leopard 中,上面的内容会给我颜色的字符串“COLORS”。

有人知道这可能是有意为之的原因吗?在 Snow Leopard 上打印 Bash 脚本中的 ANSI 转义序列的解决方法如何?

我的 Leopard 机器上的 Bash shell 版本是3.2.17(1)-release,并且3.2.48(1)-release在我的 Snow Leopard 机器上也是。

答案1

我无法告诉你为什么它不支持该参数(你可能不得不问程序员)。我只知道在我的 Linux 机器上,我得到了这个:

$ /bin/echo --help
Usage: /bin/echo [SHORT-OPTION]... [STRING]...
  or:  /bin/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 recognized:
*emphasized text*
  \0NNN   the character whose ASCII code is NNN (octal)
  \\     backslash
  \a     alert (BEL)
  \b     backspace
  \c     produce no further output
  \f     form feed
  \n     new line
  \r     carriage return
  \t     horizontal tab
  \v     vertical tab

NOTE: your shell may have its own version of echo, which usually supersedes
the version described here.  Please refer to your shell's documentation
for details about the options it supports.

Report echo bugs to [email protected]
GNU coreutils home page: <http://www.gnu.org/software/coreutils/>
General help using GNU software: <http://www.gnu.org/gethelp/>
Report echo translation bugs to <http://translationproject.org/team/>
  • 这没有提到\e逃跑
  • 它说它来自/bin/echognu coreutils。由于苹果会不时更改其 unix 系统组件的来源(例如从 zsh 移动到 bash),请检查/bin/echoLeopard 和 Snow Leopard 之间是否有变化。如果是 gnu,您可以询问 gnu.org 上的人员,为什么他们选择不包含这些序列。

至于解决方法(这更有趣):不使用/bin/echo,但 bash 的内置echo功能在 Linux 机器上有效。如果他们更改为没有内置 echo 的 bash(或更晦涩难懂的东西),您也可以尝试 shell 中这个不为人所知的功能(至少在 bash 和 zsh 中有效):

$ echo $'\e[34m''COLORS'

这是 bash 手册页的匹配部分:

   Words  of  the  form  $'string' are treated specially.  The word expands to string, with
   backslash-escaped characters replaced as specified by the ANSI  C  standard.   Backslash
   escape sequences, if present, are decoded as follows:
          \a     alert (bell)
          \b     backspace
          \e     an escape character
          \f     form feed
          \n     new line
          \r     carriage return
          \t     horizontal tab
          \v     vertical tab
          \\     backslash
          \'     single 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)
          \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 ($) will cause the string to be trans‐
   lated 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.

答案2

尝试\x1B代替\e

答案3

还能\033用吗?如果不行,您可以按 Ctrl+V,然后按 Esc 键(如果 Mac 有这些键)在命令行中创建一个真正的控制字符(当然,这在脚本中效果不太好,取决于编辑器)

答案4

为了补充现有的有用答案,背景信息

如果你是调用echo姓名仅有的- 与其路径相反,/bin/echo-你正在调用 Bash内置而不是外部实用程序。

原生 Bash 元素(如内置命令)的行为在 Bash 意义上通常是可移植的,这意味着它们在任何能够运行 Bash 的平台上都应该能正常工作

\e是一个奇怪的异常,会影响 macOS 上的普通 3.x Bash 版本(由于法律原因,它不会改变,并且 macOS 已正式将zsh其作为默认 shell)。

\e(及其别名\E应该与...合作echo -e, 鉴于\e已将支持添加到echo内置Bash 2.0,但莫名其妙地没有在 macOS 上的 3.x 库存版本中

\e 在 3.x Bash 版本上工作其他平台,例如 Windows 上的 MSYS。

反过来,如果你在 macOS 上安装并使用 Bash v4+,例如通过自制\e 工作。

相关内容