CodeRigor

LESSON

Write and run your first Bash script

Write a script of several commands, run it, print lines of text with echo and printf, add comments and read a first error message.

A program is a list of instructions for a computer, written as text. To run a program is to ask the computer to carry the instructions out.

Bash is a program that reads instructions and carries them out. It is the standard shell on Linux: the program behind a terminal, which is a window where you type an instruction, press Enter and see the result. Each instruction you type there is called a command.

A script is a text file that holds commands. That is the whole idea of this unit: a Bash script is a file of the same commands you could type at the terminal one by one. Bash reads the file and runs the commands for you, in order. Writing a script is programming.

You do not need to install anything. Every example with an Edit & run button runs right here on the page, and you can change it and run it again. Nothing you type can break anything.

One command, one line on the screen

Here is a complete script. Run it.

Bash
echo "Hello, world!"

It shows Hello, world! on the screen. The text a program shows is called its output.

Two parts of that line have names you will meet in every lesson:

  • echo is the name of a command. It shows text on the screen and then moves to a new line.
  • "Hello, world!" is the command's argument: the thing the command works on. Here it is a string, which means a piece of text. The quotation marks tell Bash where the text starts and stops. They are not part of the text, so they are not shown.

A command name and its arguments are separated by a space. echo"Hello" with no space is a different, unknown word to Bash.

From a file of commands to text on the screen
start / 3
You write commands in a text file. Bash reads the file one line at a time and runs each command. echo puts a line on the screen.
From a file of commands to text on the screen
You write commands in a text file. Bash reads the file one line at a time and runs each command. echo puts a line on the screen.

Commands run from top to bottom

A statement is one complete instruction. In a Bash script a statement normally takes one line, and so far every statement is one command. A script with three statements does three things, always in the order they are written: first line first.

Bash
echo "Fill the kettle"
echo "Boil the water"
echo "Make the tea"

This prints three lines, in the same order as the statements:

Text
Fill the kettle
Boil the water
Make the tea

Step through the diagram to watch each line run. Then swap two lines in the script above and run it again: the output changes order too, because Bash never looks ahead or rearranges your statements.

One statement at a time, top to bottom
start / 3 ⇄ code
Bash finishes each statement before it reads the next one. Every echo starts a new line on the screen.
One statement at a time, top to bottom
Bash finishes each statement before it reads the next one. Every echo starts a new line on the screen.

echo on its own, with no argument, prints an empty line. That is useful for spacing output out.

Comments are notes for people

A comment starts with #. Bash ignores everything from the # to the end of that line. Comments are for the person reading the script, including you next week.

Bash
# Show a two-line welcome.
echo "Welcome!"
echo               # an empty line
echo "Come in."

This prints Welcome!, then an empty line, then Come in.. The comments print nothing.

A # inside the quotation marks of a string is ordinary text, not a comment: echo "Room #4" prints Room #4.

The first line of a script

Scripts normally begin with this line:

Bash
#!/usr/bin/env bash
echo "This script says which program should run it."

This prints This script says which program should run it.. The first line prints nothing. It starts with #, so to Bash it is a comment. It is a note to the computer, called the shebang line (after its first two characters, #!), and it means "run this file with Bash". It only works as the very first line of the file. Start every script you write with it; the exercises in this unit do.

printf: output with exact control

echo always ends its output by moving to a new line. A second command, printf, prints exactly what you give it and nothing more. To move to a new line you write \n (a backslash followed by the letter n) inside the string. \n is not shown as two characters: it means "new line here".

Bash
printf "Tea"
printf " and toast\n"
printf "One\nTwo\n"

This prints:

Text
Tea and toast
One
Two

The first printf has no \n, so the second one continues on the same line. The third prints two lines from one command. Use echo for a plain line of text. Use printf when you need this kind of control; the next lesson uses it to place values inside a line.

Exactness matters

Bash follows your text exactly, so small differences change the result:

  • Capital letters count. echo works; Echo is a different word that Bash does not know.
  • Everything between the quotation marks is printed exactly, including spaces and punctuation. "Hello,world!" and "Hello, world!" are different strings.
  • Each string needs a quotation mark at both ends.

Read your first error message

Everyone who programs sees error messages every day. An error message is not a telling-off. It is Bash saying which line it could not follow, and why. Learning to read one is a skill in its own right.

This script has a misspelt command on line 2:

Bash
echo "one"
ehco "two"
echo "three"

Running it shows:

Text
one
solution.sh: line 2: ehco: command not found
three

Read the message from left to right. It has four parts, separated by colons:

  1. solution.sh is the name of the script file. On this site your script is always saved under that name.
  2. line 2 is the line Bash could not carry out. Go there first.
  3. ehco is the word Bash did not understand.
  4. command not found is the reason: Bash knows no command with that name.

Notice the third line of output. Bash reported the problem and then carried on with line 3. A misspelt command does not stop a script, so always read the whole output: a message in the middle means a step was skipped.

A second common mistake is a missing quotation mark:

Bash
echo "one"
echo "two
Text
solution.sh: line 2: unexpected EOF while looking for matching `"'

"EOF" stands for "end of file". Bash found a quotation mark that opens a string on line 2, kept reading to find the one that closes it, and reached the end of the file first. The line number tells you where the unfinished string starts.

This kind of mistake is called a syntax error: the text breaks the writing rules of the language, so Bash cannot tell what the commands are. This site checks a whole script for syntax errors before running it, which is why not even one is printed. (At a terminal, Bash runs the lines above the mistake first and stops when it reaches it.)

When you see an error: find the line number, go to that line, and compare it with a working example one character at a time.

Running Bash on your own computer

This is optional; the lessons do not need it. Linux and macOS have a terminal with a shell built in, and Windows can install one (search for "WSL"). Save a script in a text file named hello.sh, open a terminal in the same folder and type bash hello.sh. The output appears in the terminal, the same as it does on this page.

There is a second way, which is why the shebang line exists. Type chmod +x hello.sh once to mark the file as a program that may be run. After that, ./hello.sh runs it, and the shebang line tells the computer to use Bash.

What you can do now

You can write a script of several echo and printf commands, predict the order of its output, add comments, and find the line an error message is pointing at. In the exercises you will predict what small scripts print, repair two broken ones and then write a three-line script of your own.

Next: name values with variables, so that a script can remember and calculate things.

Watch & read

Free lectures, talks and readings that cover this skill from another angle. All resources →