← Linux and the Command Line Lesson 2 of 51 · 10 XP

echo — printing text

Theory and problem statement

Machine translation, not yet reviewed by a native speaker.

echo prints its arguments separated by spaces and moves to a new line:

echo Hello, world
echo "Text   with   spaces"     # inside quotes the spaces are kept
echo 'Single: $HOME is not expanded'
echo "Double: $HOME is expanded"

The difference between quotes is one of the main things in shell:

  • "double" — variables $name and commands $(command) are expanded inside;
  • 'single' — everything is literal, exactly as written.

Without quotes the shell splits the text into words and squeezes the spaces: echo a b prints a b.

echo with no arguments prints an empty line. For complex formatting printf is better — more on it in the module about scripts. It works the same way in all shells, while the behaviour of echo with backslashes differs from system to system.

Task. Write a script that prints three lines exactly like this:

Hello, Linux!
Spaces:   three
$USER

The last line is literally a dollar sign and the word USER, not the value of the variable.

Sign up for free to solve this task right in your browser: code editor, running, test checks and hints.