Printing with echo

echo prints text and the values of variables to the screen.

Syntaxecho [options] text

echo writes its arguments back to the terminal. It is the simplest way to display text or the contents of a variable, and it is used constantly inside scripts.

Variables and quotes

  • Inside double quotes, variables like $USER are expanded to their value.
  • Inside single quotes, everything is taken literally — $USER stays as the text $USER.

The -n option leaves off the usual trailing newline, and -e enables escape sequences such as \n for a new line and \t for a tab.

Example

Example · bash
echo "Hello, World!"
# Hello, World!

# Double quotes expand variables
echo "Logged in as $USER"
# Logged in as alice

# Single quotes are literal
echo 'Logged in as $USER'
# Logged in as $USER

# Interpret escape sequences
echo -e "Line1\nLine2"

When to use it

  • A shell script uses 'echo "Build started at $(date)"' to log progress messages to the terminal.
  • A developer appends a new environment variable with 'echo "API_URL=https://api.example.com" >> .env'.
  • A sysadmin uses 'echo $PATH' to inspect the binary search path when a command is not found.

More examples

Print text and variables

Shows echo printing a literal string and then interpolating a shell variable inside double quotes.

Example · bash
echo "Hello, World!"
# Hello, World!

NAME=Alice
echo "Welcome, $NAME!"
# Welcome, Alice!

Escape sequences with echo -e

Uses -e to interpret \n (newline) and \t (tab), making echo useful for formatted output.

Example · bash
# -e enables escape sequences
echo -e "Line 1\nLine 2\nLine 3"
# Line 1
# Line 2
# Line 3

echo -e "Column1\tColumn2"
# Column1   Column2

Append to a file with echo

Demonstrates the difference between > (overwrite) and >> (append) redirection operators with echo.

Example · bash
# Overwrite file
echo "server_name=prod" > config.ini

# Append to file
echo "port=8080" >> config.ini

cat config.ini
# server_name=prod
# port=8080

Discussion

  • Be the first to comment on this lesson.