Introduction
A shell script is a text file of commands for a Unix shell, the program that reads what is typed at a terminal. The Bourne shell of 1979 set the pattern. The KornShell, Bash and Zsh extended it, and POSIX standardised a common core.
The extension is only a convention, and many scripts have none. .sh is the
general name, usually for a script that any POSIX shell can run, while
.bash, .zsh and .ksh mark scripts written for one
shell.
Structure
A script is plain text with Unix line ends. The first line is often a shebang such as
#!/bin/sh or #!/usr/bin/env bash, which tells the system which
program should run the file when it has execute permission. A carriage return at the end
of a line, from a file saved on Windows, becomes part of the last word on that line and
breaks the script.
Each line is a command and its arguments, split on spaces. Commands connect with
|, && and ||, and > and
< redirect files. $name expands a variable,
$1 onwards are the script’s arguments, and $(command) inserts
a command’s output. Double quotes keep a value as one word. Comments start with
#.
Control flow closes with reversed words: if and fi,
case and esac, and do and done for
loops. Arrays, [[ ]] tests and many other features exist in Bash, Zsh and
KornShell but not in POSIX sh.
#!/bin/sh
# Report the size of each file named on the command line.
set -eu
for file in "$@"; do
if [ -f "$file" ]; then
size=$(wc -c < "$file")
echo "$file: $size bytes"
fi
done
Not described
Known, not yet covered
- Reference: this overview is the whole of the reference, and it gives only the outline of the language. These are not described in detail: quoting; token rules and reserved words; parameters and variables; parameter expansion and the other word expansions; pattern matching; redirection and here-documents; pipelines, lists and compound commands; conditional expressions; arithmetic; functions; arrays; the execution environment, signals and job control. The POSIX Shell Command Language and the Bash manual describe them.
- Reference: the built-in commands and the standard utilities (
test,read,trap,getopts,sed,awk,grepand the rest) are not described, nor are the differences between POSIXsh, Bash, Zsh and KornShell, the versions of each, and the startup files (.profile,.bashrc,.zshrc). - Viewer: a reserved word (
if,then,do,done,esacand the rest) is coloured only where a command may start: at the start of a line, after;,&,|,(or), or afterthen,doand the like, so the argument inecho doneis plain.inis coloured after the word of afor,selectorcase. The rule is a guess without a parser: a word after the)of$(…)is taken for the start of a command, and{and}are not read as reserved words. - Viewer: a backslash makes the next character ordinary and a here-string (
<<<) opens no here-document. Function names infoo() { … }and commands such asgrepandawkare plain, and the$of$'…'is plain, so an escaped quote inside$'it\'s'is not recognised. - Viewer: the contents of a double-quoted string, including
$(…)and${…}parts, are one string colour, and a heredoc inside$(…)is not recognised. The file is not parsed or run, and no shell is told apart: the colouring is the same for sh, Bash, Zsh and KornShell. There is no outline, no ShellCheck-style list of problems, and only the first 1 MiB is read and coloured.
Links
Software
- GNU Bash – the default shell on most Linux systems (Linux, macOS, Windows through WSL)
- Zsh – the default shell on macOS since 10.15 (macOS, Linux)
- KornShell 93 – maintained version of the KornShell (Linux, macOS, Unix)
- dash – small POSIX shell, used as
/bin/shon Debian and Ubuntu (Linux) - ShellCheck – finds bugs in shell scripts (Windows, macOS, Linux)
Developers
- mvdan/sh – shell parser and the
shfmtformatter (Go) - tree-sitter-bash – Bash grammar for the Tree-sitter parser used by editors
Reference
- Shell Command Language – the POSIX standard for
sh - Bash Reference Manual – the GNU manual
- Zsh documentation – the Zsh manual