Clone
3
echo 命令
newde edited this page 2026-03-26 09:40:16 -04:00
This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

基本用法

1. 简单文本输出

最基本的用法是直接输出字符串:

实例
echo "Hello, World!"

执行结果:
Hello, World!

2. 输出变量

echo 可以显示变量的值: 实例

name="Linux User"
echo "Welcome, $name!"

执行结果:
Welcome, Linux User!

3. 不带引号的输出

引号不是必须的,但建议使用以避免意外:

实例
echo This is a test
执行结果:
This is a test

常用选项

-n 选项:不换行输出

默认情况下echo 会在输出后添加换行符。

使用 -n 可以禁止这种行为:
实例
echo -n "Loading..."
echo " Done!"

执行结果:
Loading... Done!

-e 选项:启用转义字符解释

启用对反斜杠转义的解释:

实例
echo -e "First line\nSecond line"

执行结果:
First line
Second line

常用转义序列

转义序列 说明
\n 换行
\t 水平制表符
\v 垂直制表符
\b 退格
\r 回车
\ 反斜杠字符本身

echo -n "Progress: ["
for i in {1..20}; do echo -n "#"; sleep 0.1 ; done echo "] Done!"