回到手册索引
命令用途
echo 是一个常用的 Linux/Unix 命令,主要用于在终端输出指定的字符串或变量内容。它常用于脚本编写、调试和显示信息。
常用用法示例
输出简单的字符串
1 2
| echo "Hello, World!" Hello, World!
|
echo 将引号中的字符串直接输出到终端。
输出环境变量
1 2
| echo $HOME /home/username
|
$HOME 是一个环境变量,echo 会将其值(当前用户的主目录路径)输出。
不换行输出
1 2 3
| echo -n "Hello, " echo "World!" Hello, World!
|
-n 参数使得 echo 不会在输出后自动换行,因此两个 echo 的输出在同一行。
解释转义字符
1 2 3
| echo "This is a new line.\nNext line." This is a new line. Next line.
|
\n 是换行符,echo 默认会解释转义字符。
禁止解释转义字符
1 2
| echo -E "This is a new line.\nNext line." This is a new line.\nNext line.
|
-E 参数禁止 echo 解释转义字符,因此 \n 被当作普通字符输出。
输出带制表符的文本
1 2
| echo -e "Name\tAge" Name Age
|
-e 参数启用对转义字符的解释,\t 表示制表符。
将输出重定向到文件
1 2 3
| echo "This is a test." > test.txt cat test.txt This is a test.
|
> 符号将 echo 的输出重定向到 test.txt 文件中,cat 命令用于查看文件内容。
追加输出到文件
1 2 3 4
| echo "Another line." >> test.txt cat test.txt This is a test. Another line.
|
>> 符号将 echo 的输出追加到 test.txt 文件末尾,而不是覆盖原有内容。
常用参数选项
-n
不输出结尾的换行符。默认情况下,echo 会在输出后自动添加换行符,使用 -n 可以取消这一行为。
-e
启用对转义字符的解释。例如,\n 表示换行,\t 表示制表符等。默认情况下,echo 不会解释这些转义字符。
-E
禁止解释转义字符。这是默认行为,但可以通过显式使用 -E 来确保转义字符不会被解释。
\n
换行符。当 -e 参数启用时,\n 会被解释为换行。
\t
制表符。当 -e 参数启用时,\t 会被解释为水平制表符。
\
反斜杠本身。当 -e 参数启用时,\ 会被解释为单个反斜杠。
\c
禁止输出后续内容并取消换行。当 -e 参数启用时,\c 会终止当前行的输出,并且不会输出换行符。
\b
退格符。当 -e 参数启用时,\b 会被解释为退格操作,删除前一个字符。
原厂文档
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75
| 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)
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.
Consider using the printf(1) command instead, as it avoids problems when 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 © 2025 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'
|