Terminal GuideTerminal Guide

echo Command Guide

The echo command displays text to the terminal. It's fundamental for shell scripting and command-line output.

5 min readLast updated: 2024
Dai Aoki

Dai Aoki

CEO at init, Inc. / CTO at US & JP startups / Creator of WebTerm

Quick Reference

Basic

echo "text"Print text
echo $VARPrint variable
echo -n "text"No newline

Escape

echo -e "a\nb"Enable escapes
\nNewline
\tTab

Redirect

echo "text" > fileWrite to file
echo "text" >> fileAppend to file
echo "text" | cmdPipe to command

Common

echo $PATHShow PATH
echo $(cmd)Command output
echo {1..5}Brace expansion

Downloadable Image Preview

Failed to generate preview

Basic Usage

echo outputs the text you provide.

bash
echo "Hello, World!"
echo Hello, World!
echo 'Single quotes work too'

Common Options

echo Options

-nDo not output trailing newline
-eEnable interpretation of escape sequences
-EDisable interpretation of escape sequences (default)

Escape Sequences

Use -e to enable escape sequences.

Common Escape Sequences

\nNew line
\tHorizontal tab
\\Backslash
\aAlert (bell)
\bBackspace
\rCarriage return
bash
echo -e "Line 1\nLine 2"
# Output:
# Line 1
# Line 2

echo -e "Column1\tColumn2\tColumn3"
# Output: Column1    Column2    Column3

echo -e "Path: C:\\Windows\\System32"
# Output: Path: C:\Windows\System32

Variables and Command Substitution

bash
# Display variable
name="Alice"
echo "Hello, $name"
# Output: Hello, Alice

# Command substitution
echo "Today is $(date +%A)"
# Output: Today is Monday

# Environment variables
echo "Home directory: $HOME"
echo "Current user: $USER"
echo "Shell: $SHELL"
Tip
Use double quotes "" to expand variables. Single quotes '' treat everything literally.
bash
name="World"
echo "Hello, $name"   # Output: Hello, World
echo 'Hello, $name'   # Output: Hello, $name

No Trailing Newline

bash
# Without -n (default)
echo "Hello"; echo "World"
# Output:
# Hello
# World

# With -n
echo -n "Hello "; echo "World"
# Output: Hello World

Redirecting Output

bash
# Write to file (overwrite)
echo "Hello" > file.txt

# Append to file
echo "World" >> file.txt

# Create file with multiple lines
echo -e "Line 1\nLine 2\nLine 3" > file.txt

Colors and Formatting

bash
# ANSI color codes
echo -e "\033[31mRed text\033[0m"
echo -e "\033[32mGreen text\033[0m"
echo -e "\033[33mYellow text\033[0m"
echo -e "\033[34mBlue text\033[0m"

# Bold text
echo -e "\033[1mBold text\033[0m"

# Using variables for colors
RED='\033[0;31m'
NC='\033[0m' # No Color
echo -e "${RED}This is red${NC}"

Practical Examples

Debug output in scripts

bash
echo "DEBUG: Processing file $filename"
echo "DEBUG: Current count is $count"

Create simple configuration files

bash
echo "server=192.168.1.1" > config.txt
echo "port=8080" >> config.txt

Display separator lines

bash
echo "========================="
echo "     Script Output"
echo "========================="

Prompt for input

bash
echo -n "Enter your name: "
read name
echo "Hello, $name!"

Show special characters

bash
# Using $'...' syntax (bash)
echo $'Tab:\tNewline:\nDone'

echo vs printf

printf offers more control over formatting:

bash
# printf for precise formatting
printf "Name: %-10s Age: %d\n" "Alice" 25
# Output: Name: Alice      Age: 25

# printf is more portable
printf "%s\n" "Hello"  # Works the same everywhere
Info
For complex formatting or maximum portability, consider using printf instead of echo -e.

Summary

echo is essential for shell scripting. Key takeaways:

  • Use echo -e for escape sequences
  • Use echo -n to omit newline
  • Use double quotes for variable expansion
  • Use > to write, >> to append
  • Consider printf for complex formatting

Related Articles