Your first script
What a shell script is, how to print a line with echo and with printf, and why those two commands do not always agree about a backslash.
A script is a list of commands
A shell is the program that reads what you type at a terminal and runs it. A script is that same kind of list kept in a file, so it can be run again without being retyped. The sandbox here holds one file, main.sh, and runs it from the first line to the last. Nothing is compiled: the shell reads a line, works out what it means, and moves on.
Almost every line is a command: echo prints, printf prints with a format you choose, and later chapters add commands that search text, count lines and sort lists. A line that starts with # is a comment — a note for a reader that the shell skips entirely. A command can take arguments after its name, separated by spaces.
Two ways to print
echo takes everything after it and prints it as one line, putting a single space between the pieces. That is why echo One two three prints three words with one space each. It is the shortest way to say something. The spaces you type between words are not preserved as typed; the shell splits the line and echo joins it back with one space.
printf is the careful one. Its first argument is a pattern, and the pieces after it fill the pattern in. A backslash-n in the pattern means "start a new line here", and the shell prints exactly what the pattern says and nothing more. When a lesson needs output you can predict character for character, it reaches for printf. The pattern is reused until the values run out, which is why one %s can print a whole list.
The same line, written both ways
# echo: everything after it, joined by one space.
echo "Hello, Bash!"
echo One two three
# printf: a pattern, then the values that fill it.
printf '%s\n' "Hello, Bash!"
# %s means "a piece of text", and the pattern repeats
# for each value you hand it.
printf '%s\n' "first" "second"What the shell does with a line
Before a command runs, the shell does the line's paperwork. It splits the text into words, expands names and brace ranges, and treats the first word as the command and the rest as its arguments. That is why spacing inside a quoted string survives while the spacing between arguments becomes a single separator.
A blank line is skipped, and so is anything from a hash to the end of its line. The script is not compiled or checked in advance, so a mistake on line nine does not stop lines one to eight — worth remembering when only half of the expected output appears. The shell reports the failing line and leaves the earlier output on screen.
Order, and what a line leaves behind
Lines run in the order they appear, each finishing before the next starts. Every command also leaves a number behind, its exit code, and the next chapter is about reading it. For now the useful part is that a script is a sequence, not a conversation: it cannot wait for you to type an answer. Anything the script needs must be written in the file or produced by a command before it is used.
printf writes its pattern to standard output and repeats that pattern until the values run out. One %s with three values therefore prints three lines, which is why it suits lists as well as single sentences. A pattern with no values still prints once; more values than markers repeats the whole pattern.
Reading a script out loud
A script is easier to check when you read it as a sequence of instructions rather than as code. "Print a greeting, then print three words, then print a formatted line" is the whole of the first example. If a line does not fit that sentence, it is either doing two jobs or relying on a value the reader cannot see.
The same habit catches most first-day mistakes. A missing quote turns the rest of the file into one long argument, so the output stops making sense at the line where the quote was left open. A command that produces no output may have succeeded silently, and its exit code is the place to look next.
Your first script, line by line
Press Run and read the output. Then change the greeting, add a line of your own, and replace one `echo` with a `printf` that prints the same text.
A script does not need the #! line that starts some files, and one here would change nothing. Keep every line under sixty characters or so as you write: shell lines get read on small screens too, and a line that runs sideways is a line nobody finishes.
Three habits prevent most first-script surprises: no spaces around = in an assignment, no assumption that echo treats backslashes the same way in every shell, and always end a printf pattern with \n yourself, because printf adds no newline for you. When two lines of output have run together, the missing \n is the first thing to check.
Try these next
- Print three words with one
printfcall, using a single%s\npattern and three values. - Add a second comment line and confirm that it changes nothing in the output.
- Print a blank line first with
echo "", then withprintf "\n".
A table, built by one pattern
Run it, then add a fourth row and a total line. Keep the columns aligned by giving every `%s` the same width.
Check what you learned
One question at a time. A wrong answer explains itself — take another go.
Part 1 of 5
What is a shell script?
