Skip to content

Exercise 2 of 10 · Variables

Lost and Found

What you will make

A four-line notice for a station lost-and-found desk, with the item, the place it was found and the collection desk each filled in from a variable.

The one new idea: Variables: name=value with no spaces, $name to use it, and single quotes stop expansion

Variables are how a script stops repeating itself: set a value once, use it in five places. But the shell's rules for setting and reading them are strict in ways other languages are not, and item = value, $item and '$item' are three of the most common beginner mistakes in bash. Meeting them now, on a tiny script, saves an afternoon later.

Go straight to the code ↓

Naming a value

A variable is a name for a piece of text. You create one by writing the name, =, and the value, with no spaces on either side of the =:

Shell
film="Night Shift"

That spacing rule is not a style choice. If you write film = "Night Shift", the shell reads film as a command to run and = as its first argument — and then reports that there is no command called film.

To use the value, put $ in front of the name: $film. Inside double quotes the shell swaps $film for its value before echo ever sees the line. Inside single quotes it does not: '$film' prints the four characters $film, dollar sign included. Single quotes mean "exactly this, touch nothing".

A worked example

Shell
film="Night Shift"
screen=12
echo "Now showing: $film"
echo "Screen $screen"
echo 'Screen $screen'
Output
Now showing: Night Shift
Screen 12
Screen $screen

The first two lines show the values. The third shows what single quotes do: the $screen stayed exactly as written. Notice also that the value Night Shift needed quotes when it was assigned, because it has a space in it; 12 did not.

Your turn

The editor holds a lost-and-found notice built from three variables: the item, where it was found, and which desk is holding it. It should print a heading and three sentences, each with the right value filled in.

Three lines are marked. One assignment has spaces around its =. One echo names the variable without a $, so the word item is printed instead of the scarf. One echo uses single quotes, so $desk is printed as-is rather than being replaced.

Fix each of the three and run it. The output should read as a proper notice with no $ signs and no bare variable names in it.

If something goes wrong

item: command not found means the assignment line still has spaces around the =. Close the gap so the name, the = and the value touch.

If a line prints the word item or desk instead of the value, either the $ is missing or the line is wrapped in single quotes. Use double quotes whenever you want a $name filled in.

If a value with a space in it prints only its first word, the assignment lost its quotes. item=blue scarf hands blue to a command called scarf as a temporary setting — and there is no such command.

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

Writing item = "blue scarf" with spaces around the equals sign
The shell reads the first word of a line as a command, so this runs a command called item with = and the scarf as its arguments. There is no such command, and no variable gets set. An assignment has no spaces on either side of the =.
Forgetting the $ when using the variable
Without the $, item is just the four letters i-t-e-m and the shell prints them as they are. The $ is the signal that says replace this name with its value.
Using single quotes around text that contains $desk
Single quotes switch off every kind of substitution: $desk stays as five characters. Double quotes keep spaces but still replace $names with values, which is what you want almost every time you print a variable.
Dropping the quotes from a value that has a space in it
item=blue scarf does not set item to blue scarf. The shell reads it as a temporary setting item=blue for a command called scarf, which does not exist. A value with spaces in it needs quotes when it is assigned.

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