首页·终端命令

echo

文本处理

用法

echo [text]

原样打印参数并换行;-e 和反斜杠的处理在不同 shell 里不一样,要稳妥就用 printf。

常用选项

选项含义
-nLeave off the trailing newline
-eRead backslash escapes such as \n. Not in every shell
-EDo not read escapes, which is the usual default

示例

echo -n done

Leaves the cursor on the same line.

echo "$PATH"

Prints a variable; the quotes keep spaces intact.

echo *

The shell expands this to the file names, not echo.

它们本来就是用管道串起来用的。串三条往往比让一条命令做完所有事更短。

怎么看

  • 方括号 [ ] 表示这部分可以不写。
  • 省略号 … 表示可以写多个。
  • 选项区分大小写——有些命令里 -r 和 -R 不是一回事。

常见问题

Q. echo 是做什么的?

原样打印参数并换行;-e 和反斜杠的处理在不同 shell 里不一样,要稳妥就用 printf。

Q. 怎么写?

echo [text] —— 方括号表示可以省略的部分。

Q. 值得记的选项有几个?

这里列了 3 个,完整列表在 man echo。这条命令属于文本处理。

相关命令

man 手册: man echo