Echo Command in Linux
https://phoenixnap.com/kb/echo-command-linux
Introduction
The echo command is a built-in Linux feature that prints out arguments as the standard output. echo
is commonly used to display text strings or command results as messages.
In this tutorial, you will learn about all the different ways you can use the echo command in Linux.

Prerequisites
- A system running Linux
- Access to the terminal window/command line
Echo Command Syntax
The echo
command in Linux is used to display a string provided by the user.
The syntax is:
echo [option] [string]
For example, use the following command to print Hello, World!
as the output:
echo Hello, World!

Note: Using the echo
command without any option returns the provided string as the output, with no changes.
Echo Command Options
Use the --help
argument to list all available echo
command options:
/bin/echo --help

Note: Using the echo --help
command returns --help
as the output.
The echo
command uses the following options:
-n
: Displays the output while omitting the newline after it.-E
: The default option, disables the interpretation of escape characters.-e
: Enables the interpretation of the following escape characters:- \\: Displays a backslash character (\).
\a
: Plays a sound alert when displaying the output.\b
: Creates a backspace character, equivalent to pressing Backspace.\c
: Omits any output following the escape character.\e
: The escape character, equivalent to pressing Esc.\f
: The form feed character, causes the printer to automatically advance to the start of the next page.\n
: Adds a new line to the output.\r
: Performs a carriage return.\t
: Creates horizontal tab spaces.\v
: Creates vertical tab spaces.\NNN
: Byte with the octal value ofNNN
.\xHH
: Byte with the hexadecimal value ofHH
.
Examples of Echo Command
Here are some ways you can use the echo
command in Linux:
Changing the Output Format
Using the -e
option allows you to use escape characters. These special characters make it easy to customize the output of the echo command.
For instance, using \c
let you shorten the output by omitting the part of the string that follows the escape character:
echo -e 'Hello, World! \c This is PNAP!'

Note: If you are using the -e
option, enter your string enclosed in single quotation marks. This ensures that any escape characters are interpreted correctly.
Use \n
any time you want to move the output to a new line:
echo -e 'Hello, \nWorld, \nthis \nis \nPNAP!'

Add horizontal tab spaces by using \t
:
echo -e 'Hello, \tWorld!'

Use \v
to create vertical tab spaces:
echo -e 'Hello, \vWorld, \vthis \vis \vPNAP!'

Using ANSI escape sequences lets you change the color of the output text:
echo -e '\033[1;37mWHITE'
echo -e '\033[0;30mBLACK'
echo -e '\033[0;31mRED'
echo -e '\033[0;34mBLUE'
echo -e '\033[0;32mGREEN'

Writing to a File
Use >
or >>
to include the string in an echo
command in a file, instead of displaying it as output:
sudo echo -e 'Hello, World! \nThis is PNAP!' >> test.txt
If the specified text file doesn’t already exist, this command will create it. Use the cat
command to display the content of the file:
cat test.txt

Note: Using >
overwrites the content of the text file with the new string, while >>
adds the new string to the existing content.
Displaying Variable Values
The echo
command is also used to display variable values as output. For instance, to display the name of the current user, use:
echo $USER

Displaying Command Outputs
The echo
command allows you to include the result of other commands in the output:
echo "[string] $([command])
Where:
[string]
: The string you want to include withecho
[command]
: The command you want to combine with theecho
command to display the result.
For instance, list all the files and directories in the Home directory by using:
echo "This is the list of directories and files on this system: $(ls)"

Conclusion
After reading this tutorial, you should know how to use the echo
command in Linux.
For more Linux commands, check out our Linux Command Cheat Sheet
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧
· 【自荐】一款简洁、开源的在线白板工具 Drawnix
· 园子的第一款AI主题卫衣上架——"HELLO! HOW CAN I ASSIST YOU TODAY
· 无需6万激活码!GitHub神秘组织3小时极速复刻Manus,手把手教你使用OpenManus搭建本
2019-07-17 Java – Check if Array contains a certain value?
2019-07-17 mysql教程,linux, shell
2019-07-17 Android Activity的Launch Mode
2019-07-17 sqlite3 not found/not executable:32-bit ELF file解决办法
2019-07-17 python基础
2019-07-17 数据结构(Python实现)
2018-07-17 Android开发之如何保证Service不被杀掉(前台服务)