What Bash is and how it works
Bash is the GNU Project’s shell: a program that reads commands from a terminal or a file and executes them. It serves both as the working environment in a Linux terminal and as a scripting language that understands the classic sh syntax and extends it.
Bash is the default shell on Linux. On macOS, /bin/bash is the long-standing Bash 3.2, and many of the features covered in this reference are missing from it: install a recent Bash from Homebrew or MacPorts. On Windows, Bash runs in WSL — the Windows Subsystem for Linux — or in Cygwin.
Bash processes every line according to the same scheme:
- Reads the input: a line from the terminal, a script file, or the argument of
bash -c. - Splits the input into words and operators according to the quoting rules and expands aliases.
- Assembles the words into simple and compound commands.
- Performs the expansions — in the order shown in the diagram.
- Performs the redirections and removes them from the list of arguments.
- Runs the command and, unless it runs in the background, waits for its exit status.
The order of expansions is strict. First comes brace expansion. Then, from left to right, come tilde, parameter and variable expansion, arithmetic and command substitution. Last come word splitting, filename expansion and quote removal. Only brace expansion, word splitting and filename expansion can increase the number of words, and so can "$@" and "${arr[@]}". Only what expands outside double quotes is split into words: $opts yields two arguments, while "$f" yields one.
opts='-l -h', f='my file', with app.log and err.log in the directory. What each stage changed is highlighted.How Bash differs from sh
Bash understands the grammar, expansions, redirections and quoting of the classic Bourne shell (sh) and adds a great deal of its own. Run a script that uses the constructs from the table with Bash itself: sh does not have them.
| Bash feature | Example |
|---|---|
The [[ ]] conditional with glob patterns and regular expressions | [[ $file == *.log ]] |
| Arrays | hosts=(web1 web2) |
| Brace expansion | mkdir -p app/{src,tests} |
Arithmetic (( )) and the for (( )) loop | (( total = a + b )) |
| Here-string and process substitution | grep -c x <<< "$s", diff <(sort a.txt) <(sort b.txt) |
| Replacement and substring in a variable’s value | ${version//./_}, ${name:0:3} |
Prerequisites
- Linux basics: files and directories, permissions, processes — the Linux reference.
- The
grep,sed,awkandsortutilities — the Linux reference, the section “Text processing: grep, sed, awk”.
Section sources
the section is written from these pages- Shell Expansionsgnu.org/software/bash/manual/html_node/Shell-Expansions.html(opens in a new tab)
- Shell Operationgnu.org/software/bash/manual/html_node/Shell-Operation.html(opens in a new tab)
- Word Splittinggnu.org/software/bash/manual/html_node/Word-Splitting.html(opens in a new tab)
- Major Differences From The Bourne Shellgnu.org/software/bash/manual/html_node/Major-Differences-From-The-Bourne-Shell.html(opens in a new tab)
- The GNU Bourne-Again Shelltiswww.case.edu/php/chet/bash/bashtop.html(opens in a new tab)