Back to Blog

Linux Commands: The echo Command - Print Text, Variables & More (Beginner Tutorial)

Sandy LaneSandy Lane

Video: Linux Commands: The echo Command - Print Text, Variables & More (Beginner Tutorial) by Taught by Celeste AI - AI Coding Coach

Watch full page →

Linux Commands: The echo Command - Print Text, Variables & More (Beginner Tutorial)

The echo command is a fundamental Linux tool used to display text and variable values in the terminal. This tutorial covers basic usage, handling quotes, variable expansion, escape sequences, and redirecting output to files.

Code

# Print simple text with a newline
echo "Hello World"

# Print text without a trailing newline using -n
echo -n "Hello without newline"

# Define a variable
name="Alice"

# Print variable value with double quotes (expands variable)
echo "Hello, $name"

# Print literal text with single quotes (no expansion)
echo 'Hello, $name'

# Use environment variables
echo "Home directory: $HOME"
echo "Current user: $USER"

# Use escape sequences with -e flag: newline (\n) and tab (\t)
echo -e "Line1\nLine2"
echo -e "Column1\tColumn2"

# Redirect output to a file (overwrite)
echo "This is a line" > output.txt

# Append output to the same file
echo "This is another line" >> output.txt

# View file contents
cat output.txt

Key Points

  • echo prints text followed by a newline by default; use -n to suppress the newline.
  • Double quotes allow variable expansion, while single quotes print text literally without expanding variables.
  • The -e flag enables interpretation of escape sequences like \n (newline) and \t (tab).
  • Use > to redirect output to a file (overwrite) and >> to append to a file.
  • echo is essential for scripting, debugging, and displaying dynamic output in the terminal.