Operations 6 min read

15 Powerful Echo Tricks to Supercharge Your Linux Scripts

This guide walks through fifteen practical and advanced uses of the Linux echo command, covering basic text output, disabling newlines, escape sequences, tabular formatting, backspace, alert sounds, carriage‑return overwrites, wildcard expansions, file redirection, variable injection, colorized text, and dynamic progress bars, all illustrated with ready‑to‑run examples.

Liangxu Linux
Liangxu Linux
Liangxu Linux
15 Powerful Echo Tricks to Supercharge Your Linux Scripts

Overview

The echo command is one of the most fundamental tools in Linux, used to print text or variable values to the terminal. Beyond simple output, it offers a range of advanced features that can greatly enhance shell scripts and system‑administration tasks.

1. Output Fixed Text

$ echo "欢迎学习高级 echo 技巧"
欢迎学习高级 echo 技巧

2. Suppress Trailing Newline (-n)

$ echo -n "输出不换行"
输出不换行$

3. Enable Escape Sequences (-e)

$ echo -e "换行测试
第二行文本"
换行测试
第二行文本

4. Print Tabular Data (\t)

$ echo -e "名称\t性别\t分数
Alice\t女\t90
Bob\t男\t85"
名称    性别    分数
Alice   女      90
Bob     男      85
Table example
Table example

5. Simulate Backspace (\b)

$ echo -e "错误\b正确"
正确

6. Emit Alert Sound (\a)

$ echo -e "\a任务完成"
(plays a beep if the system volume permits)

7. Overwrite a Line with Carriage Return (\r)

$ echo -e "进度:100%\r完成"
完成

8. List All Files in Current Directory (*)

$ echo *
file1.txt file2.txt script.sh

9. List Files of a Specific Type (*.sh)

$ echo *.sh
script.sh deploy.sh

10. Redirect Output to a File (>)

$ echo "Hello, World" > example.txt
$ cat example.txt
Hello, World

11. Append to a File (>>)

$ echo "追加内容" >> example.txt
$ cat example.txt
Hello, World
追加内容

12. Inject Variables Dynamically

$ name="Alice"
$ echo "欢迎, $name!"
欢迎, Alice!

13. Truncate a File (>>)

$ echo > log.txt

14. Colored Output with ANSI Escape Sequences

$ echo -e "\033[31m这是红色\033[0m"
(prints red text)

Here \033[31m sets red color and \033[0m resets the style.

15. Build a Dynamic Progress Bar

for i in {1..10}; do
  echo -ne "进度: $i/10\r"
  sleep 1
done
echo -ne "
任务完成
"

Flexibility & Practicality : echo can output plain text, special characters, and colored strings to improve script readability.

System Integration : Combined with variable injection and redirection, echo helps handle configuration, logging, and chaining of commands.

Broad Compatibility : It is natively supported by all Unix/Linux environments.

By mastering these fifteen techniques, you can unlock the hidden potential of echo and significantly boost your Linux administration efficiency.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

LinuxShellcommand-lineUnixEcho
Liangxu Linux
Written by

Liangxu Linux

Liangxu, a self‑taught IT professional now working as a Linux development engineer at a Fortune 500 multinational, shares extensive Linux knowledge—fundamentals, applications, tools, plus Git, databases, Raspberry Pi, etc. (Reply “Linux” to receive essential resources.)

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.