Skip to content

Exercise 1 of 10 · Output and quoting

Trail Signpost

What you will make

A neatly aligned trailhead signpost: a header, three destinations with their distances in a straight column, and a footer, printed by five echo commands.

The one new idea: echo prints one line, and double quotes keep spacing exactly as typed

Every shell script talks to you through echo, and the first thing the shell does to every line is split it into words on spaces. Until you know that, output that looks almost right will keep surprising you. Quoting is the habit that makes shell scripts predictable, and it starts on line one.

Go straight to the code ↓

What echo does

A shell script is a list of commands, one per line. echo is the command that prints text: everything after the word echo is written to the screen, followed by a new line. That last part matters — echo always ends the line for you, so five echo commands give five lines.

The shell reads each line before running it. It splits the line into words wherever it sees spaces, and then hands those words to the command. Three spaces or one — the shell does not care, it just sees a gap between words. So if you write a run of spaces without quotes, they collapse to one. Double quotes stop that: everything inside "..." is handed over as a single piece, spaces and all.

A worked example

Two lines that look nearly the same:

Shell
echo "Bus stop:   Elm Road"
echo Bus stop:   Elm Road
Output
Bus stop:   Elm Road
Bus stop: Elm Road

The first line kept its three spaces because they were inside quotes. The second lost them: the shell split Bus, stop:, Elm and Road into four separate words, and echo simply printed the words with one space between each.

Notice also that there is no command called Bus. If the first word of a line is not a command, the shell stops and reports it — which is exactly what happens when you forget to write echo at all.

Your turn

The editor holds a signpost for a walking trail: a header, three lines of destinations with distances lined up in a neat column, and a footer. It should print five lines, all with the distances aligned.

Two lines are marked. On one, the quotes have gone missing, so the distance is no longer lined up with the others. On the other, the whole echo is missing — the shell will try to run === as a command and complain that it does not exist.

Put the quotes back on the first one, put echo (and quotes) back on the second, and run it. Compare your output with the expected output line by line, including the spaces.

If something goes wrong

If you see command not found, look at the first word of the line it names. That word is being treated as a command to run. It probably needs echo in front of it.

If the words are right but the spacing is not, the line is almost certainly missing its double quotes. The grader compares spaces too, so Lake 1.5 km and Lake 1.5 km are different answers.

Write your code

Runs in your browser. Press Run (or Ctrl/Cmd+Enter) and the output is checked for you.

Ctrl/Cmd+Enter to run

Press Esc then Tab to move keyboard focus out of the code editor.

Ready
Output will appear here after you run your code.

The runtime is starting in the background. You can type now — it will be ready before you are.

The answer appears here once you have run your code at least once.

Things that often go wrong here

Quoting only part of the Lake line, such as the name but not the distance
Quotes protect only what is inside them. If the run of spaces is outside the quotes, the shell still splits there and the column is still out of line. Put the whole message, spaces included, inside one pair of double quotes.
Fixing the spacing by adding more spaces without adding quotes
Any number of unquoted spaces collapses to one. The shell does not count them; it only sees a gap between words. Only quotes preserve the run of spaces you typed.
Typing the footer with echo but leaving the old comment text in the quotes
The comment after the line is fine to leave, but anything inside the quotes is printed. If the text in the quotes is not exactly === Happy trails ===, the last line will not match.
Using single quotes on one line and double quotes on another
Here both kinds of quotes give the same result, because none of the text contains a $ sign. That is fine for this exercise, but from the next exercise on the two kinds behave differently, so it is worth getting used to double quotes now.

Want a blank editor instead? Open the Bash & the Shell playground.