国际访客建议访问 Primers 编程伙伴 国际版站点 > Bash 教程 > echo 以获得更好的体验。

# Bash 的 echo 命令

echo [OPTION]... [STRING]...

!subtitle:功能

显示一行文本。

!subtitle:类型

Bash 内置命令。

echo 也是 coreutils 的一个可执行文件;为了保持 POSIX 标准的兼容性,确保在其他 shell(比如一些极简的或古老的 shell)或者某些特定环境下,命令依然可用。

!subtitle:说明

  • 选项

    • -n - 不在末尾自动添加换行

    • -e - 启用转义字符

    • -E - 禁用转义字符

  • 转义字符(开启 -e 选项时有效)

    • \\ - 表示反斜杠 \ 本身

    • \a - 响铃

    • \b - 退格

    • \c - 不再产生输出

    • \e - ANSI 转义代码

    • \f - 换页符

    • \n - 换行符

    • \r - 回车符

    • \t - 水平制表符

    • \v - 垂直制表符

    • \0NNN - 八进制字符编码

    • \xXXX - 十六进制字符编码

# ANSI 转义代码

使用 ANSI 转义代码的格式为 \e[代码;代码;代码m:可以同时包含任意数量的代码,以分号(;)分隔,并以 m 结束。

代码 说明 备注
0 重置 清除所有效果,恢复默认
1 强调(加粗)
2 弱化
3 斜体
4 下划线
5 缓慢闪烁
6 快速闪烁 未被广泛支持
7 反显(前景色与背景色交换)
8 隐藏 未被广泛支持
9 删除线 未被广泛支持
10 主要字体
11-19 替代字体
20 德文尖角体 未被广泛支持
21 关闭粗体或双下划线 未被广泛支持
22 正常颜色强度
23 非斜体、非尖角体
24 关闭下划线 消除 4 和 21
25 关闭闪烁 消除 5 和 6
27 关闭反显 消除 7
28 关闭隐藏 消除 8
29 关闭删除线 消除 9
30-37 设置前景色 颜色代码
38 设置前景色 后跟 5;n(256 色模式) 或 2;r;g;b(24 位 RGB 模式)
39 恢复默认前景色 消除 30-38
40-47 设置背景色 颜色代码
48 设置背景色 后跟 5;n(256 色模式) 或 2;r;g;b(24 位 RGB 模式)
49 恢复默认背景色 消除 40-48
51 边框 未被广泛支持
52 包围 未被广泛支持
53 上划线 未被广泛支持
55 关闭上划线 消除 53
60 表意文字下划线或右边线 未被广泛支持
61 表意文字双下划线或双右边线 未被广泛支持
62 表意文字上划线或左边线 未被广泛支持
63 表意文字双上划线或双左边线 未被广泛支持
64 表意文字强调 未被广泛支持
65 关闭表意文字属性 消除 60-64
90-97 设置明亮的前景色 aixterm 非标准,见 颜色代码
100–107 设置明亮的背景色 aixterm 非标准,见 颜色代码

# 颜色代码

颜色 前景色代码 背景色代码 备注
30 40 !embed:<div style="width:100%;height:16px;background:#000000"/>
31 41 !embed:<div style="width:100%;height:16px;background:#800000"/>
绿 32 42 !embed:<div style="width:100%;height:16px;background:#008000"/>
33 43 !embed:<div style="width:100%;height:16px;background:#808000"/>
34 44 !embed:<div style="width:100%;height:16px;background:#000080"/>
35 45 !embed:<div style="width:100%;height:16px;background:#800080"/>
36 46 !embed:<div style="width:100%;height:16px;background:#008080"/>
37 47 !embed:<div style="width:100%;height:16px;background:#c0c0c0"/>
亮黑(灰) 90 100 !embed:<div style="width:100%;height:16px;background:#808080"/>
亮红 91 101 !embed:<div style="width:100%;height:16px;background:#ff0000"/>
亮绿 92 102 !embed:<div style="width:100%;height:16px;background:#00ff00"/>
亮黄 93 103 !embed:<div style="width:100%;height:16px;background:#ffff00"/>
亮蓝 94 104 !embed:<div style="width:100%;height:16px;background:#0000ff"/>
亮紫 95 105 !embed:<div style="width:100%;height:16px;background:#ff00ff"/>
亮青 96 106 !embed:<div style="width:100%;height:16px;background:#00ffff"/>
亮白 97 107 !embed:<div style="width:100%;height:16px;background:#ffffff"/>

# 256 色模式

$include(/resource/bash/256-color.html)

# 示例

运行示例

# 显示文本
echo https://xplanc.org/

# 加粗 前景黄色 背景红色
echo -e "\e[1;33;41mhttps://xplanc.org/\e[0m"

# 使用 256 色模式,前景色是 201 背景色是 51
echo -e "\e[38;5;201;48;5;51mhttps://xplanc.org/\e[0m"

# 使用 24 位 RGB 模式,前景色是 255,255,0 背景色是 0,0,255
echo -e "\e[38;2;255;255;0;48;2;0;0;255mhttps://xplanc.org/\e[0m"

# 推荐阅读

# 手册

ECHO(1)                          User Commands                         ECHO(1)

NAME
       echo - display a line of text

SYNOPSIS
       echo [SHORT-OPTION]... [STRING]...
       echo LONG-OPTION

DESCRIPTION
       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:

       \\     backslash

       \a     alert (BEL)

       \b     backspace

       \c     produce no further output

       \e     escape

       \f     form feed

       \n     new line

       \r     carriage return

       \t     horizontal tab

       \v     vertical tab

       \0NNN  byte with octal value NNN (1 to 3 digits)

       \xHH   byte with hexadecimal value HH (1 to 2 digits)

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

       NOTE: printf(1) is a preferred alternative, which does not have  issues
       outputting option-like strings.

AUTHOR
       Written by Brian Fox and Chet Ramey.

REPORTING BUGS
       GNU coreutils online help: <https://www.gnu.org/software/coreutils/>
       Report any translation bugs to <https://translationproject.org/team/>

COPYRIGHT
       Copyright  ©  2023  Free Software Foundation, Inc.  License GPLv3+: GNU
       GPL version 3 or later <https://gnu.org/licenses/gpl.html>.
       This is free software: you are free  to  change  and  redistribute  it.
       There is NO WARRANTY, to the extent permitted by law.

SEE ALSO
       printf(1)

       Full documentation <https://www.gnu.org/software/coreutils/echo>
       or available locally via: info '(coreutils) echo invocation'

GNU coreutils 9.4                 April 2024                           ECHO(1)
本文 更新于: 2025-11-27 09:38:14 创建于: 2025-11-27 09:38:14