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.
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 valueParameter 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 textExample 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 3Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
Open Source Linux
Focused on sharing Linux/Unix content, covering fundamentals, system development, network programming, automation/operations, cloud computing, and related professional knowledge.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
