The echo command is one of the most widely used built-in commands across Linux, Bash, and POSIX shells. It prints lines of text, variables, or formatted strings directly to the standard output (stdout), a terminal screen, or a file through I/O redirection.
The table below details the essential command-line flags and backslash escape sequences supported by echo:
Sr.No |
Option / Sequence |
Descriptions |
|---|---|---|
01 |
|
Do not print the trailing newline (leaves cursor on the same line). |
02 |
|
Enable interpretation of backslash escape characters (such as |
03 |
|
Disable interpretation of backslash escapes (default behavior in Bash). |
04 |
|
Backspace: erases the preceding character. |
05 |
|
Newline: moves cursor to the beginning of the next line. |
06 |
|
Horizontal Tab: inserts standard tab spacing. |
07 |
|
Vertical Tab: moves down one line while preserving column position. |
08 |
|
Carriage Return: moves cursor back to the start of the line without advancing. |
09 |
|
Suppress further output from that point forward (omits newline). |
10 |
|
Prints a literal backslash character. |
#!/bin/bash
# Normal echo adds a newline at the end
echo "Welcome to Veewom Technology"
# Echo with -n leaves cursor at the prompt for user input
echo -n "Enter user password: "
read password
-e#!/bin/bash
# IMPORTANT: Backslash escapes require the -e flag in Bash!
echo -e "Line 1: Linux Basics\nLine 2: Shell Scripting\nLine 3: Advanced C"
# Tab-separated columns
echo -e "ID\tNAME\tCOURSE\n101\tRahul\tBCA\n102\tPriya\tMCA"
One of the most valuable capabilities of echo -e is formatting text with colors, bold fonts, and background highlights using ANSI escape sequences:
Color / Style |
ANSI Code |
Example Usage |
|---|---|---|
Red (Errors) |
|
|
Green (Success) |
|
|
Yellow (Warnings) |
|
|
Blue (Info) |
|
|
Reset |
| Mandatory at the end to restore normal terminal text color! |
You can write or append string data directly into files or streams using shell redirection operators:
# Overwrite (truncate) file with string
echo "server_ip=192.168.1.100" > /tmp/config.conf
# Append new line to existing file
echo "port=8080" >> /tmp/config.conf
# Redirect message to Standard Error (stderr)
echo "Fatal: Invalid configuration file" >&2
echo vs printfWhile echo is quick for simple terminal messages, professional scripts frequently employ the POSIX printf command because printf behavior is 100% identical across all Unix platforms, shells, and operating systems:
# printf uses C-style formatting without requiring -e
printf "User: %-10s | Score: %5d | Avg: %6.2f\n" "Rohan" 95 98.45
Problem: Write a shell script that checks system load and displays a formatted dashboard with colored alerts using echo -e.
#!/bin/bash
# server_dashboard.sh: Formatted status display
# Define color constants
RED='\e[31m'
GREEN='\e[32m'
YELLOW='\e[33m'
CYAN='\e[36m'
BOLD='\e[1m'
NC='\e[0m' # No Color / Reset
clear
echo -e "${BOLD}${CYAN}==================================================${NC}"
echo -e "${BOLD}${CYAN} LINUX SERVER HEALTH MONITOR CONSOLE ${NC}"
echo -e "${BOLD}${CYAN}==================================================${NC}"
echo -e "Hostname : $(hostname)"
echo -e "Timestamp : $(date)"
echo -e "Kernel Ver : $(uname -r)"
echo -e "--------------------------------------------------"
# Simulate service checks
echo -e -n "Checking Web Server (Nginx) ... "
echo -e "${GREEN}[RUNNING]${NC}"
echo -e -n "Checking Database (MySQL) ... "
echo -e "${GREEN}[RUNNING]${NC}"
echo -e -n "Checking Firewall (UFW) ... "
echo -e "${YELLOW}[ACTIVE - 4 Rules]${NC}"
echo -e -n "Checking Disk Free Space ... "
free_space=$(df -h / | awk 'NR==2 {print $5}')
echo -e "${YELLOW}[${free_space} USED]${NC}"
echo -e "${BOLD}${CYAN}==================================================${NC}"
-e option to tell Bash to interpret backslash escapes: echo -e "First\nSecond". Without -e, Bash prints literal \n.
-n option: echo -n "Enter username: ". The prompt remains on the same line so the user can type right next to it.
> to overwrite the file (e.g. echo "text" > output.txt) or double greater-than >> to append to the end of the file.
echo is built into shells with minor variations between UNIX flavors (e.g. BSD vs GNU). printf follows the standard POSIX specification, allowing exact formatting precision (like %d, %s, and field widths) across all shells.
Capture interactive user inputs, passwords, and file lines in Class 18: Read Command.
Continue to Class 18: Read Command → Previous: ← Variables | Full Syllabus