echo Command Guide
The echo command displays text to the terminal. It's fundamental for shell scripting and command-line output.
5 min read•Last updated: 2024
Dai Aoki
CEO at init, Inc. / CTO at US & JP startups / Creator of WebTerm
Quick Reference
Basic
echo "text"Print textecho $VARPrint variableecho -n "text"No newlineEscape
echo -e "a\nb"Enable escapes\nNewline\tTabRedirect
echo "text" > fileWrite to fileecho "text" >> fileAppend to fileecho "text" | cmdPipe to commandCommon
echo $PATHShow PATHecho $(cmd)Command outputecho {1..5}Brace expansionDownloadable 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
| -n | Do not output trailing newline |
| -e | Enable interpretation of escape sequences |
| -E | Disable interpretation of escape sequences (default) |
Escape Sequences
Use -e to enable escape sequences.
Common Escape Sequences
| \n | New line |
| \t | Horizontal tab |
| \\ | Backslash |
| \a | Alert (bell) |
| \b | Backspace |
| \r | Carriage 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\System32Variables 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, $nameNo Trailing Newline
bash
# Without -n (default)
echo "Hello"; echo "World"
# Output:
# Hello
# World
# With -n
echo -n "Hello "; echo "World"
# Output: Hello WorldRedirecting 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.txtColors 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.txtDisplay 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 everywhereInfo
For complex formatting or maximum portability, consider using
printf instead of echo -e.Summary
echo is essential for shell scripting. Key takeaways:
- Use
echo -efor escape sequences - Use
echo -nto omit newline - Use double quotes for variable expansion
- Use
>to write,>>to append - Consider
printffor complex formatting