Linux › Section K · Shell scripts › Topic K.1
Script basics, quoting, and literals
A shell script is a file of the same commands you type at the prompt, saved so that it can run as a program. Most of the bugs that puzzle new script writers come from one habit of the shell, rewriting a command line before it runs, and quoting is how you take control of that.
From typed commands to a script
A shell script is a plain text file of commands that a shell reads and runs in order, just as if you had typed them. A loop over files, a pipeline or a redirection into a log can be saved once and repeated with one command. Scripts for the Bourne shell family, sh and its descendants bash and dash, are the most portable, and they are what this section teaches, building on "The Bourne shell and using it" and "Shell input and output: redirection and pipes". Every example in this section was run in GNU bash 5.2, and the outputs shown are real.
The shebang line
The first line names the program that will interpret the script. It begins with #!, the shebang, followed by an absolute path, such as #!/bin/sh or #!/bin/bash. The kernel starts that interpreter and passes it the script's path, so running ./tidy with #!/bin/sh is much like typing /bin/sh ./tidy. The #! must be the first two bytes of the file; after a blank line or a space the kernel ignores it, and whichever shell launched the file runs it instead. #!/usr/bin/env bash makes env find bash on your PATH, useful when bash lives in different places. Choose deliberately: on Debian and Ubuntu /bin/sh is dash, while on Fedora and RHEL it is bash, so a script using bash-only features must name bash.
Comments, long lines and permissions
A # that begins a word starts a comment that runs to the end of the line; inside a word it is ordinary, so echo a#b c #note prints a#b c. A backslash as the last character of a line joins it to the next; with a stray space after it, the backslash escapes the space instead and bash runs the next line as a separate command. A script needs execute permission and, because the interpreter opens it, read permission: chmod 755 tidy (rwxr-xr-x), or chmod 700 tidy (rwx------) for a private one. Then run ./tidy, its full path, or just tidy if it sits in a PATH directory such as /usr/local/bin. Plain tidy in the current directory gives command not found, because that directory is not on the PATH.
What the shell does before a command runs
Every command line passes through two stages. First, the shell expands it: it replaces variables such as $HOME and command substitutions with their values, splits the results into words, and expands globs such as *.txt into matching filenames. Second, it runs the command with the words it produced as arguments. The command never sees what you typed. That explains a classic surprise: echo $100 prints 00, because the shell reads $1, the first positional parameter, which is empty at a fresh prompt, followed by the text 00. A literal is a string you want passed through untouched, and quoting is how you make one.
Worked example: a pattern eaten by a glob
A directory holds users.txt, with the lines root:x:0:0, ruth:x:1001 and bob:x:5, plus two empty files, r.input and r.output. You want lines with an r followed later by a t. 1. Run grep r.*t users.txt. It prints nothing and exits with status 1, no match. 2. Put set -x before the same command in a script; bash then prints each command after expansion: + grep r.input r.output users.txt. 3. Read the trace: the glob matched both filenames, so grep searched r.output and users.txt for the text r.input. 4. Quote the pattern: grep 'r.*t' users.txt prints root:x:0:0 and ruth:x:1001. 5. Check: bob has no r, so it is rightly left out. The unquoted form works only while no filename matches, which is why such bugs seem to come and go.
Single quotes: everything literal
Between single quotes the shell changes nothing: no variables, no globs, no backslash escapes, and spaces do not split words. So echo '$100' prints $100. Everything inside becomes one argument, which can bite: grep 'r.*t users.txt' hands grep the single pattern r.*t users.txt and no filename, so grep waits to read standard input. Single quotes are the first choice for any literal, because nothing inside them can surprise you. Their one limit is that they cannot contain a single quote.
Double quotes: variables in, globs out
Double quotes also make one argument and also block globbing and word splitting, but the dollar sign still works: variables, $(command) and $((arithmetic)) expand. With name=Ada, echo "No * here, $name" prints No * here, Ada; the single-quoted version prints $name as written, and the unquoted one replaces * with every filename present. Inside double quotes a backslash escapes only $, `, ", \ and newline. Double quotes also preserve spacing: with msg="two spaces", echo $msg prints two spaces, because the value is split into two words, but echo "$msg" keeps all three spaces. Writing "$var" is the safe default.
Backslashes and literal single quotes
Outside quotes, a backslash makes the next character literal: echo I don\'t mind prints I don't mind, and \* passes a real asterisk. Inside single quotes a backslash is just a character, so 'don\'t panic' escapes nothing: the quoted part ends after don\, and the final quote opens a string that never closes. bash stops with unexpected EOF while looking for matching `'' and exit status 2. When nothing needs protecting from expansion, double quotes are the easy fix: "I don't mind." For a string that must stay fully literal, change every ' into '\'' (close the quotes, add an escaped quote, reopen), then wrap the whole string in single quotes.
Worked example: quoting an awkward string
Print exactly: it isn't a slash: \ 1. The text holds a single quote and a backslash, and nothing should expand, so single quotes are the base. 2. Apply the rule: isn't becomes isn'\''t. 3. Wrap the string: 'it isn'\''t a slash: \'. 4. Run echo with that argument; bash prints it isn't a slash: \ as required. 5. Check how the shell read it: three pieces with no spaces between them join into one word, 'it isn' (quoted), \' (an escaped quote) and 't a slash: \' (quoted, so the backslash is literal). To count arguments, printf '<%s>' prints each in angle brackets: a "b c" gives <a><b c>, two arguments, while a b c gives three.
Common mistakes to avoid
First, a blank line or space before #!, so the wrong shell runs the script. Second, a missing execute bit, or expecting a script in the current directory to run without ./. Third, unquoted patterns and variables, so globs and word splitting quietly change the arguments. Fourth, single quotes where you want expansion: echo 'Hello $USER' prints Hello $USER. Fifth, trying to escape a quote inside single quotes. Sixth, bash-only features under #!/bin/sh, which fail where /bin/sh is dash.
Key takeaways
A script is a text file of shell commands whose first line, the shebang, such as #!/bin/sh or #!/usr/bin/env bash, names the interpreter and starts at the first byte. Make it readable and executable, for example with chmod 755, and run it as ./name or from a PATH directory. The shell expands variables, command substitutions and globs before a command sees its arguments. Single quotes make everything literal; double quotes allow $ expansions but block globs and word splitting; a backslash escapes one character; '\'' puts a single quote inside a literal.
Flashcards
Flashcard
A text file of shell commands that a shell runs in order
Test your understanding
Question 1 of 5
A directory contains files named r.input and r.output. Why does the command grep r.*t users.txt print nothing?
Question 2 of 5
In bash, with name=Ada, what does echo 'Hi $name' "Hi $name" print?
Question 3 of 5
Which command prints exactly I don't mind. with no error?
Question 4 of 5
A script starts with #!/bin/sh and uses features that only bash supports. It works on Fedora but fails on Ubuntu. What is the most likely reason?
Question 5 of 5
With msg="two spaces" (three spaces), what does echo $msg print, and why?
