echo command in Linux: print text and variables

Updated November 1, 2026

The echo command prints its arguments to standard output.

Syntax

echo [-n] text...

Common cases

echo hello world          # prints the line
echo "$HOME"              # a variable's value
echo one two three > f.txt # output to a file
echo -n no-newline        # no trailing newline

Sample output

$ echo hello world
hello world
$ echo "$HOME"
/home/student
$ echo '$HOME'
$HOME
$ echo *
a.txt b.txt

How it works: quotes and spaces

echo joins its arguments with a single space, so extra spaces between words are lost. Double quotes "$x" keep a value as one piece and expand variables; single quotes '$x' print the text literally, with no expansion. For portable output with escape sequences people use printf, which behaves the same everywhere.

Common mistakes

The shell expands the asterisk, not echo. In a directory that holds a.txt and b.txt, echo * prints a.txt b.txt and no star reaches the output. To print the character itself, quote it: echo '*'.

echo "a\nb" in bash prints a\nb as written. The builtin echo does not read \n until you pass -e. But /bin/sh on Ubuntu is dash, and the same line inside a #!/bin/sh script prints two lines. One command, two results, depending on which shell runs it. printf "a\nb\n" prints the same thing in both.

echo --help prints --help. The builtin knows -n, -e and -E, and treats everything else as text to print, so this is the one command whose help you cannot get that way. For builtins the help command is help: help echo.

Used in

You need this in the lesson The command line. The theory is the chapter The command line: first contact.

See also

Anatomy of a command, Redirection > and >>.

Try it

Open the sandbox: a real throwaway Linux terminal in your browser. Run the commands above by hand - the container is deleted when you leave.