Operations 4 min read

Mastering the Linux echo Command: Options, Syntax, and Colorful Output Tricks

Learn how to use the Linux echo command to display strings and variables, explore its syntax and options like -n and -e, and discover advanced techniques for colored, formatted, and escaped output through practical examples.

Open Source Linux
Open Source Linux
Open Source Linux
Mastering the Linux echo Command: Options, Syntax, and Colorful Output Tricks

echo: Output specified strings or variables

Function Description

echo command is used in the shell to print the value of a variable or directly output a specified string. It is extremely common in shell programming and frequently used to display variable values in the terminal.

Command Syntax

echo [options] [arguments]

Option meanings

-n : Do not output the trailing newline.

-e : Enable interpretation of backslash-escaped characters.

When using the -e option, the following escape sequences are recognized:

\a alert (bell)
\b backspace (delete previous character)
\c suppress further output
\f form feed (move to next line, keep cursor)

 newline (move cursor to start of next line)
\r carriage return (move cursor to start of line)
\t horizontal tab
\v vertical tab (same as \f)
\\ backslash

nn octal byte value

Parameter meaning

Parameters refer to variables whose values are to be printed.

Reference examples

Example 1

Print colored text with echo:

// text color:
echo -e "\e[1;31mMy font color is red\e[0m"
My font color is red
// background color:
echo -e "\e[1;42mMy background is green\e[0m"
My background is green
// flashing text:
echo -e "\033[37;34;5mI am blinking blue text\033[39;49;0m"
I am blinking blue text

Example 2

// output variable value
echo $PATH
/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin

// escape $ to prevent variable expansion
echo \$PATH
$PATH

// execute command substitution and print result
echo `date`
Wed Sep 22 20:44:21 CST 2021

// redirect output to a file
echo "www.linuxyz.cn" > www.html

// output with newline characters
echo -e "1
2
3"
1
2
3

// output with tab characters
echo -e "1\t2\t3"
1       2       3
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.

LinuxBashterminalcommand-lineEcho
Open Source Linux
Written by

Open Source Linux

Focused on sharing Linux/Unix content, covering fundamentals, system development, network programming, automation/operations, cloud computing, and related professional knowledge.

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.