Linux Commands: The echo Command - Print Text, Variables & More (Beginner Tutorial)
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
echoprints text followed by a newline by default; use-nto suppress the newline.- Double quotes allow variable expansion, while single quotes print text literally without expanding variables.
- The
-eflag 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. echois essential for scripting, debugging, and displaying dynamic output in the terminal.