Linux CLI 14 🐧 echo command
echo is one of the simplest yet most versatile commands. It displays text, expands wildcards, evaluates variables, does basic arithmetic, and even interprets escape sequences.
Basic Usage
echo "Hello, World!"
Output:
Hello, World!
echo is used to display one or more lines of text to the terminal.
Key points:
- Most shells have
echoas a builtin (fast, built-in command) - There’s also an external
/bin/echo(may behave differently) - Extremely common in scripts and pipelines
- Accepts multiple arguments — they’re printed separated by spaces
Multiple Arguments
$ echo Hello World
Hello World
$ echo "Hello World"
Hello World
$ echo Hello World
Hello World
Note: echo collapses multiple spaces into one when using unquoted arguments. Quotes preserve them.
$ echo "Hello World"
Hello World
The -e Option — Enable Escape Sequences
echo -e "Line 1\nLine 2"
Output:
Line 1
Line 2
Without -e, escape sequences are printed literally:
$ echo "Line 1\nLine 2"
Line 1\nLine 2
$ echo -e "Line 1\nLine 2"
Line 1
Line 2
Escape Sequences Supported by echo -e
| Escape | Meaning | Example |
|---|---|---|
\n | New line | echo -e "Line 1\nLine 2" |
\t | Tab | echo -e "Name:\tJohn" |
\\ | Backslash | echo -e "C:\\Users" |
\a | Alert (bell) | echo -e "\a" |
\b | Backspace | echo -e "abc\bd" |
\c | Suppress trailing newline | echo -e "Text\c" |
\e | Escape character | echo -e "\e[31mRed\e[0m" |
\f | Form feed | echo -e "Page 1\fPage 2" |
\r | Carriage return | echo -e "Loading...\rDone" |
\v | Vertical tab | echo -e "A\vB" |
\0NNN | Octal value | echo -e "\0101" → A |
\xHH | Hex value | echo -e "\x41" → A |
\uHHHH | Unicode | echo -e "\u03A9" → Ω |
Practical Escape Examples
Tab-separated output:
$ echo -e "Name:\tJohn\nAge:\t25"
Name: John
Age: 25
Multiple lines:
$ echo -e "Line 1\nLine 2\nLine 3"
Line 1
Line 2
Line 3
Escaping special characters:
$ echo "Escaping \t and \\"
Escaping \t and \\
$ echo -e "Escaping \t and \\"
Escaping and \
Colored output:
$ echo -e "\e[31mError:\e[0m Something went wrong"
Error: Something went wrong
# (Error: appears in red)
The -n Option — No Trailing Newline
echo -n "No New Line Here"
Output:
No New Line Here$
(where $ is the next prompt — no newline before it)
Default vs -n:
$ echo "Hello"
Hello
$ # prompt on new line
$ echo -n "Hello"
Hello$ # prompt on same line!
Useful for prompts:
$ echo -n "Enter your name: "
Enter your name: _
Example with counter:
$ for i in 1 2 3; do
echo -n "$i "
done
1 2 3 $
The -E Option — Disable Escape Interpretation
echo -E "Line 1\nLine 2"
Output:
Line 1\nLine 2
-E forces backslash interpretation off — the opposite of -e.
Why it exists: In some shells, echo interprets escapes by default. Use -E to force literal output.
# On some systems, this might interpret \n automatically
$ echo "Line 1\nLine 2"
# Force literal with -E
$ echo -E "Line 1\nLine 2"
Line 1\nLine 2
Note: On bash, echo does not interpret escapes by default — so -E is rarely needed. It’s more relevant on other shells.
Echo as a Calculator
echo $((2*6))
Output:
12
Uses arithmetic expansion ($((...))) to do math before printing.
Arithmetic Examples
$ echo $((5 + 3))
8
$ echo $((10 - 4))
6
$ echo $((3 * 7))
21
$ echo $((20 / 4))
5
$ echo $((17 % 5))
2
$ echo $((2 ** 10))
1024
$ echo $((5 > 3))
1
$ echo $((5 < 3))
0
Combining with text:
$ echo "2 times 6 is $((2*6))"
2 times 6 is 12
$ echo "The result of 5 + 3 is $((5+3))"
The result of 5 + 3 is 8
With variables:
$ a=10
$ b=5
$ echo "$a + $b = $((a + b))"
10 + 5 = 15
Supported operators:
| Operator | Meaning |
|---|---|
+ | Addition |
- | Subtraction |
* | Multiplication |
/ | Division |
% | Modulo |
** | Exponentiation |
>, <, >=, <= | Comparison (returns 0 or 1) |
==, != | Equality (returns 0 or 1) |
&&, || | Logical AND, OR |
! | Logical NOT |
Echo with Wildcards
echo *
echo d*
echo *1
echo /home/kronos/*/d2
Wildcards are expanded by the shell before echo sees them.
| Command | Description |
|---|---|
echo * | Print all files/directories in current dir |
echo d* | Print all files starting with d |
echo *1 | Print all files ending with 1 |
echo /home/kronos/*/d2 | Print paths matching the pattern |
Examples
All files:
$ ls
apple.txt banana.txt cherry.txt docs images
$ echo *
apple.txt banana.txt cherry.txt docs images
Starting with a letter:
$ echo b*
banana.txt
Ending with a number:
$ echo *1
file1 report1
Path patterns:
$ echo /home/kronos/*/d2
/home/kronos/projects/d2 /home/kronos/archive/d2
Why use echo with wildcards?
- Testing — see what a pattern matches before running
rm - Debugging — verify glob behavior
- Scripts — pass matched files to other commands
⚠️ Safety tip:
$ echo *.log # ✅ Safe — just prints matches
$ rm *.log # ⚠️ Deletes matches!
# Always test with echo first
Echo with Variables
echo ~
echo $USER
| Command | Description |
|---|---|
echo ~ | Print user’s home directory |
echo $USER | Print the $USER variable |
echo $HOME | Print home directory |
echo $PATH | Print PATH variable |
echo $SHELL | Print current shell |
Examples
Home directory:
$ echo ~
/home/kronos
Environment variables:
$ echo $USER
kronos
$ echo $HOME
/home/kronos
$ echo $SHELL
/bin/bash
$ echo "Hello, $USER!"
Hello, kronos!
Multiple variables:
$ echo "User: $USER, Home: $HOME, Shell: $SHELL"
User: kronos, Home: /home/kronos, Shell: /bin/bash
Quoting matters:
$ echo $USER
kronos
$ echo "$USER"
kronos
$ echo '$USER'
$USER # ← single quotes prevent expansion
$ echo "\$USER"
$USER # ← escaped $ also prevents expansion
Displaying All Environment Variables
printenv
Shows all OS environment variables:
$ printenv
SHELL=/bin/bash
USER=kronos
HOME=/home/kronos
PATH=/usr/local/bin:/usr/bin:/bin
PWD=/home/kronos
LANG=en_US.UTF-8
...
Related commands:
| Command | Description |
|---|---|
printenv | Show all environment variables |
printenv PATH | Show a specific variable |
env | Similar — also used to run commands in modified environments |
set | Show all shell variables (including non-exported) |
export | Mark a variable for export to child processes |
Complete Example Session
# ============================================
# PART 1: BASIC ECHO
# ============================================
$ echo "Hello, World!"
Hello, World!
$ echo Hello World
Hello World
$ echo Hello World
Hello World
$ echo "Hello World"
Hello World
# ============================================
# PART 2: ESCAPE SEQUENCES (-e)
# ============================================
# Without -e (literal)
$ echo "Line 1\nLine 2"
Line 1\nLine 2
# With -e (interpreted)
$ echo -e "Line 1\nLine 2"
Line 1
Line 2
# Tabs
$ echo -e "Name:\tJohn\nAge:\t25"
Name: John
Age: 25
# Escaping backslash
$ echo "Escaping \t and \\"
Escaping \t and \\
$ echo -e "Escaping \t and \\"
Escaping and \
# Colors
$ echo -e "\e[32mSuccess!\e[0m"
Success!
# (green text)
# Bell
$ echo -e "\a"
# (terminal bell)
# ============================================
# PART 3: NO NEWLINE (-n)
# ============================================
$ echo -n "No New Line Here"
No New Line Here$
# Useful for prompts
$ echo -n "Enter name: "
Enter name: _
$ read name
John
$ echo "Hello, $name"
Hello, John
# ============================================
# PART 4: CALCULATOR
# ============================================
$ echo $((2*6))
12
$ echo $((5 + 3))
8
$ echo $((2 ** 10))
1024
$ echo "The sum of 5 and 3 is $((5 + 3))"
The sum of 5 and 3 is 8
# With variables
$ a=10
$ b=20
$ echo "$a + $b = $((a + b))"
10 + 20 = 30
# ============================================
# PART 5: WILDCARDS
# ============================================
$ ls
apple.txt banana.txt cherry.txt docs images
$ echo *
apple.txt banana.txt cherry.txt docs images
$ echo *.txt
apple.txt banana.txt cherry.txt
$ echo b*
banana.txt
$ echo /home/kronos/*/d2
/home/kronos/projects/d2
# ============================================
# PART 6: VARIABLES
# ============================================
$ echo ~
/home/kronos
$ echo $USER
kronos
$ echo $HOME
/home/kronos
$ echo "Current user: $USER, Home: $HOME"
Current user: kronos, Home: /home/kronos
$ echo '$USER is a variable'
$USER is a variable
$ echo "\$USER is a variable"
$USER is a variable
# ============================================
# PART 7: PRINTENV
# ============================================
$ printenv
SHELL=/bin/bash
USER=kronos
HOME=/home/kronos
PATH=/usr/local/bin:/usr/bin:/bin
PWD=/home/kronos
LANG=en_US.UTF-8
...
$ printenv PATH
/usr/local/bin:/usr/bin:/bin
$ printenv USER
kronos
# ============================================
# PART 8: PRACTICAL USES
# ============================================
# Progress indicator
$ for i in {1..5}; do echo -n "."; sleep 0.5; done; echo " Done"
..... Done
# Log entry with timestamp
$ echo "[$(date)] Backup started" | tee -a backup.log
[Mon Jan 15 10:30:00 UTC 2024] Backup started
# Debug variables
$ echo "DEBUG: HOME=$HOME, USER=$USER"
DEBUG: HOME=/home/kronos, USER=kronos
# File list preview
$ echo /var/log/*
/var/log/alternatives.log /var/log/apt /var/log/auth.log ...
# Colored warnings
$ echo -e "\e[33mWARNING:\e[0m Disk usage is high"
WARNING: Disk usage is high
Quick Reference
echo Options
| Option | Description |
|---|---|
| (none) | Display text |
-e | Enable escape sequences |
-n | No trailing newline |
-E | Disable escape interpretation |
Common Escape Sequences (-e)
| Escape | Meaning |
|---|---|
\n | New line |
\t | Tab |
\\ | Backslash |
\a | Bell |
\r | Carriage return |
\e | Escape char |
\xHH | Hex value |
\uHHHH | Unicode |
\0NNN | Octal |
Arithmetic Operators
| Operator | Meaning |
|---|---|
+ | Add |
- | Subtract |
* | Multiply |
/ | Divide |
% | Modulo |
** | Power |
> < == | Comparison |
&& || ! | Logical |
Common Variables
| Variable | Description |
|---|---|
$USER | Current username |
$HOME | Home directory |
$PWD | Current directory |
$SHELL | Current shell |
$PATH | Command search path |
Best Practices
✅ Do This:
# Use quotes for literal text
echo "Hello, World!"
# Use -e for escape sequences
echo -e "Line 1\nLine 2"
# Use -n for prompts
echo -n "Enter name: "
# Preview glob matches before rm
echo *.log # Check first
rm *.log # Then delete
# Use $((...)) for arithmetic
echo "Result: $((5 * 3))"
# Quote variables
echo "$USER" # ✅ Expands
# Use single quotes for literal $
echo '$HOME' # ✅ Prints $HOME
# Use printenv for env inspection
printenv PATH
❌ Don’t Do This:
# Don't forget -e for escapes
echo "Line 1\nLine 2" # ❌ Literal \n
echo -e "Line 1\nLine 2" # ✅ Interpreted
# Don't use echo for complex output (use printf)
echo -e "\e[31mColored" # ⚠️ Works but printf is better
printf "\033[31mColored\033[0m\n" # ✅ More portable
# Don't trust echo for user data
echo "$USER_INPUT" # ⚠️ May interpret escapes
printf '%s\n' "$USER_INPUT" # ✅ Safer
# Don't use echo for binary data
echo -e "\x00\x01\x02" # ⚠️ May not work
printf '\x00\x01\x02' # ✅ Correct
# Don't use echo for multiline content in scripts
echo "Line 1
Line 2" # ⚠️ Depends on shell
# Use a heredoc instead
cat << EOF
Line 1
Line 2
EOF
Common Pitfalls
| Pitfall | Problem | Solution |
|---|---|---|
echo without -e | Literal \n shown | Add -e |
echo -e not portable | Different on other shells | Use printf |
| Spaces collapsed | Extra spaces removed | Use quotes |
| Single vs double quotes | Variable not expanded | Use " for expansion |
echo * before rm * | Don’t skip this step! | Always preview first |
echo with binary data | Garbled output | Use printf or cat |
echo vs printf
For serious scripting, printf is often better:
| Aspect | echo | printf |
|---|---|---|
| Portability | Varies between shells | POSIX standard |
| Escape handling | -e needed | Always interprets |
| Formatting | None | Full format specifiers |
| Binary data | Unreliable | Reliable |
| Newline control | -n | \n or omit |
Examples:
# echo
echo -e "Name:\t$USER\nHome:\t$HOME"
# printf (equivalent)
printf "Name:\t%s\nHome:\t%s\n" "$USER" "$HOME"
# printf with formatting
printf "%-10s: %5d\n" "Score" 42
# Score : 42
# printf for binary
printf '\x00\x01\x02' > binary.bin
Real-World Examples
1. Log Entry with Timestamp
echo "[$(date '+%Y-%m-%d %H:%M:%S')] User $USER logged in" >> /var/log/auth.log
2. Progress Indicator
echo -n "Processing"
for i in {1..10}; do
echo -n "."
sleep 0.2
done
echo " Done!"
3. Generate a Config File
{
echo "# Config file"
echo "user=$USER"
echo "home=$HOME"
echo "generated=$(date)"
} > config.txt
4. Colored Error Messages
echo -e "\e[31mERROR:\e[0m File not found"
echo -e "\e[33mWARNING:\e[0m Disk space low"
echo -e "\e[32mSUCCESS:\e[0m Operation complete"
5. Debug Script Variables
#!/bin/bash
set -x # Print each command
echo "DEBUG: PATH=$PATH"
echo "DEBUG: Running from $(pwd)"
6. Environment Variable Check
if [ -z "$JAVA_HOME" ]; then
echo "JAVA_HOME is not set"
echo "Please set it in ~/.bashrc"
exit 1
fi
7. Multi-Line Report
echo -e "===== Report =====\n\
Date: $(date)\n\
User: $USER\n\
Directory: $(pwd)\n\
=================="
Summary
| Feature | Syntax | Example |
|---|---|---|
| Display text | echo "text" | echo "Hello" |
| Escape sequences | echo -e "..." | echo -e "Line\n2" |
| No newline | echo -n "..." | echo -n "Prompt: " |
| Arithmetic | echo $((...)) | echo $((5 * 3)) |
| Wildcards | echo * | echo *.txt |
| Variables | echo $VAR | echo $USER |
| All env vars | printenv | printenv PATH |
Key takeaways:
echodisplays text and is a shell builtin-eenables escape sequences like\n,\t,\\-nsuppresses the trailing newline — great for prompts-Edisables escape interpretation (rarely needed in bash)- Use
$((...))for arithmetic — no external calculator needed - Wildcards are expanded by the shell before
echosees them $VARIABLEexpands environment variablesprintenvshows all environment variables- For portable, reliable scripting, prefer
printfoverecho
Remember: echo is one of the first commands everyone learns — but it’s also one of the most misunderstood. Its behavior varies between shells, its escape handling requires -e on bash, and it can produce surprising results with special characters. Use it for quick output, debugging, and simple scripts. When you need precision — especially in production scripts — reach for printf. And always remember: echo * before rm * — it might just save your files!
Stop using slow, ad-bloated tool sites! 🤮
🔎 Search “KandZ Tools” on Google to use many professional utilities for free.
KandZ.me is the ultimate minimalist hub for:
✅ Finance (Mortgage, Interest, Inflation)
✅ Tech (Base64, JSON, Dev Suite, IP)
✅ Health (BMI, BMR, TDEE)
✅ Productivity (Timer, Workspace, QR)
⚡️ Fast & Private
🔒 No data leaves your device
💎 100% Free
🔗 Use it now: https://tools.kandz.me
🔖 Bookmark it—you’ll need it later!