echo Command

The echo command displays text to the terminal or redirects it to files. It's one of the most fundamental commands in Linux, commonly used for printing messages, displaying variables, and creating output in shell scripts.

Syntax

echo [OPTION]... [STRING]...

Description

The echo command outputs the given strings to standard output, followed by a newline. It's essential for shell scripting, displaying messages, showing variable values, and creating simple text output.

Key features:

  • Display text and messages
  • Print variable values
  • Support escape sequences
  • Redirect output to files
  • Built-in shell command

Common Options

Option Description
-n Do not output trailing newline
-e Enable interpretation of backslash escapes
-E Disable interpretation of backslash escapes (default)

Escape Sequences (with -e option)

Sequence Description
\\ Backslash
\a Alert (bell)
\b Backspace
\c Suppress trailing newline
\f Form feed
\n Newline
\r Carriage return
\t Horizontal tab
\v Vertical tab

Examples

Display simple text

echo "Hello, World!"

Outputs: Hello, World!

Display without quotes

echo Hello World

Outputs: Hello World

Display variables

echo $USER echo "Current user: $USER"

Displays the username and a formatted message

Display without trailing newline

echo -n "Enter your name: "

Prompts for input without moving to next line

Use escape sequences

echo -e "Line 1\nLine 2\nLine 3"

Outputs three lines using newline escape sequence

Tab-separated output

echo -e "Name\tAge\tCity" echo -e "John\t25\tNew York"

Creates tab-separated columns

Display special characters

echo -e "Bell sound: \a" echo -e "Backslash: \\"

Demonstrates bell sound and backslash display

Multiple arguments

echo "Today is" $(date) echo The current directory is $(pwd)

Combines text with command output

Redirect to file

echo "Hello World" > output.txt echo "Second line" >> output.txt

Creates and appends to a file

Display environment variables

echo "Home directory: $HOME" echo "Shell: $SHELL" echo "Path: $PATH"

Shows common environment variables

Colorized output (with ANSI codes)

echo -e "\033[31mRed text\033[0m" echo -e "\033[32mGreen text\033[0m" echo -e "\033[34mBlue text\033[0m"

Displays colored text using ANSI escape codes

Create simple progress indicator

echo -n "Processing" for i in {1..5}; do echo -n "." sleep 1 done echo " Done!"

Creates a simple progress indicator

Display array elements

fruits=("apple" "banana" "orange") echo "Fruits: ${fruits[@]}" echo "First fruit: ${fruits[0]}"

Shows how to display array contents

Common Use Cases

  • Shell Scripting: Display messages and status updates
  • Variable Display: Show environment variables and script variables
  • File Creation: Create simple text files with content
  • Debugging: Print debug information in scripts
  • User Interaction: Display prompts and messages
  • Configuration: Generate configuration files

Shell Scripting Examples

#!/bin/bash # Script header echo "=== System Information Script ===" echo # Display system info echo "Hostname: $(hostname)" echo "Current user: $USER" echo "Current date: $(date)" echo "Uptime: $(uptime -p)" # Conditional output if [ -f "/etc/os-release" ]; then echo "OS: $(grep PRETTY_NAME /etc/os-release | cut -d'"' -f2)" fi echo echo "Script completed successfully!"

Example shell script using echo for information display

See also