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.
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:
echois 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.
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.
echo "Fill the kettle"
echo "Boil the water"
echo "Make the tea"This prints three lines, in the same order as the statements:
Fill the kettle
Boil the water
Make the teaStep 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.
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.
# 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:
#!/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".
printf "Tea"
printf " and toast\n"
printf "One\nTwo\n"This prints:
Tea and toast
One
TwoThe 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.
echoworks;Echois 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:
echo "one"
ehco "two"
echo "three"Running it shows:
one
solution.sh: line 2: ehco: command not found
threeRead the message from left to right. It has four parts, separated by colons:
solution.shis the name of the script file. On this site your script is always saved under that name.line 2is the line Bash could not carry out. Go there first.ehcois the word Bash did not understand.command not foundis 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:
echo "one"
echo "twosolution.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.