{"canonical_url":"https://engineer.company/glossary/bash/","content_markdown":"# Bash\n\n## What it is\n\nBash, the Bourne-Again SHell, distributed as GNU Bash and started as the `bash`\ncommand, is a program that reads commands, typed at a prompt or saved in a file,\nand carries them out: it starts other programs, connects their input and output,\nand adds variables, loops, tests and functions so that a sequence of commands\ncan make decisions. The name is a pun on Stephen Bourne, whose `sh` shipped with\nthe Seventh Edition of Research Unix at Bell Labs. Its manual describes Bash as\nlargely compatible with that shell, with features taken from the Korn shell and\nthe C shell, and as intended to conform to the shell part of the POSIX standard.\nThe current release is 5.3, and the 5.3 manual is dated May 2025.\n\nA shell is easy to confuse with the window it runs in: the terminal draws\ncharacters and forwards keystrokes, and the shell is the program on the other\nside that interprets them. Bash runs interactively, with a prompt, history and\ntab completion, or non-interactively, reading a script from top to bottom\nunattended, which is where most of its traps live.\n\nBash is one of several programs that can fill the same role. POSIX defines a\nportable shell language, `sh`, and the program installed as `/bin/sh` varies by\nsystem. On Debian and Ubuntu it is dash, a small POSIX shell descended from the\nAlmquist shell, and Debian Policy names bash, dash and ksh as the most common\nshells to use there. Zsh has been the default login shell for new accounts on\nmacOS since version 10.15, although the Mac still carries a `/bin/bash`. A\nscript picks its interpreter with its first line, the shebang: `#!/bin/sh` asks\nfor whatever POSIX shell the system has, while `#!/bin/bash` or\n`#!/usr/bin/env bash` asks for Bash by name.\n\n## Why it matters\n\nBash is the connecting language of Unix servers. It is on almost every Linux\nmachine, and the commands an engineer types at a prompt are already Bash, so a\nhandful of them saved in a file becomes a script that runs on what the machine\nalready has. For chaining existing programs, such as dumping a database,\ncompressing the dump and copying it elsewhere, few languages are as brief,\nbecause pipes, redirection and exit statuses are its native vocabulary.\n\nThe same traits turn into costs as a script grows. Every value is a string, and\narithmetic works only on integers, so anything with structure, such as JSON,\ndates or decimal amounts, has to be handed to another tool. Unquoted expansions\nare split into words and then matched against filenames, so a value containing a\nspace or an asterisk changes the meaning of the command it lands in. Error\nhandling is opt-in: by default a script carries on after a command fails, and\nthe options that change that, `set -e` and `pipefail`, come with exceptions the\nmanual needs several paragraphs to list. A workable rule is that once a script\nneeds data structures, a parser or a test suite, it has become a program, and a\nlanguage such as Python is cheaper to maintain.\n\nPortability is a decision as well. Debian Policy requires `/bin/sh` scripts to\nkeep to POSIX features plus a short list of extensions, and asks any script that\nneeds more to name its shell on the first line, for example `#!/bin/bash`. It\nsuggests `checkbashisms` or a run under a stricter shell such as posh to find\nviolations, and advises `/bin/bash` when in doubt. The standard moves slowly:\nBash has offered `pipefail` since version 3.0, and POSIX added it only in its\n2024 edition.\n\nA shell turns text into commands, which makes it a security boundary wherever\noutside text reaches it. Shellshock, recorded as CVE-2014-6271 in September\n2014, showed how wide that boundary can be. Bash up to version 4.3 executed\ncommands that trailed a function definition in an environment variable, so\nanything that copied attacker-controlled text into the environment before\nstarting Bash, including CGI scripts under Apache, the ForceCommand feature of\nOpenSSH and some DHCP clients, became a way to run code on the machine. The\noriginal fix was incomplete, and what remained received its own identifier,\nCVE-2014-7169. The everyday lesson is smaller: outside text reaches a command as\nan argument, never as part of a string handed to `eval` or `sh -c`.\n\n## What it looks like in practice\n\nAcross the company's work, Bash keeps the job the [tech stack](/tech-stack/)\nassigns it: Python for data work, automation and services, and Bash for the\nglue, written to be read.\n\nIn the\n[six ETL and ELT pipelines](/portfolio/designed-and-ran-6-etl-elt-pipelines/)\nthat fed Google BigQuery from a data warehouse behind an IPsec VPN, the\ndownloaded archives held Parquet, CSV and .bak files. Bash and Python scripts\nunpacked them and classified each file by type and column layout before bcp, psql and\nSSIS loaded them into staging schemas and gzip, Google Cloud Storage and the bq\ntool carried the results into BigQuery. With the unpacking and sorting scripted,\na run that had taken three hours or more by hand finished in under 20\nminutes, and the data could be synced every day instead of once a week.\n\nThe\n[hundred automated critical data backups](/portfolio/automated-100-critical-data-backups-using-barman/)\nshow the same division of labour. Barman handled the PostgreSQL side, Google\nCloud held the offsite copies, and Bash and Python orchestrated and validated\nthe whole regime, with retention policies keeping the copies current and\nintegrity checks confirming that each one was good rather than merely present.\nThat is the job Bash suits: a sequence of existing tools, where the script's\nvalue lies in checking what each step returned.\n\n## Where it goes wrong\n\n**A filename with a space becomes two arguments.** An unquoted `$file` is split\non whitespace and then expanded as a glob, so `report 2024.csv` reaches `rm` as\ntwo names and a stray `*` matches a whole directory. Quote every expansion, as\nin `\"$file\"`, pass argument lists on as `\"$@\"`, keep lists of names in arrays,\nand let ShellCheck find the rest.\n\n**A backup pipeline reports success after the dump has failed.** A pipeline's\nexit status is that of its last command, so in `pg_dump app | gzip \u003e app.sql.gz`\na failed dump still leaves gzip succeeding on whatever little it received. Turn\non `set -o pipefail`, which makes the pipeline return the last non-zero status,\nand check the result with `gzip -t` or, better, a test restore.\n\n**A script with `set -e` carries on past a failed command.** The option is\nignored in the test of an `if` or `while`, in every part of an `\u0026\u0026` or `||` list\nexcept the last, and inside any function called from those places, so the dump\nbelow can fail and the script still prints its success message:\n\n```bash\n#!/usr/bin/env bash\nset -euo pipefail\n\nbackup() {\n  pg_dump app \u003e /var/backups/app.sql\n  gzip -f /var/backups/app.sql\n}\n\nif backup; then # set -e is ignored inside backup here\n  echo \"backup done\"\nfi\n```\n\nA related trap is `local out=$(cmd)`, which returns the status of `local` and\nhides a failure in `cmd`. Inside functions, follow each command that matters\nwith `|| return 1`, and declare a variable before assigning to it on a separate\nline.\n\n**A script that works on one machine breaks on another.** A file that starts\nwith `#!/bin/sh` but uses `[[` tests or arrays runs under Bash on one system and\nunder dash on the next. There `[[` is an unknown command, so an `if` built on it\nprints an error and takes its else branch, and an array assignment is a syntax\nerror that ends the script. On a Mac, `/bin/bash` reports version 3.2.57, older\nthan the associative arrays and `mapfile` that arrived in 4.0, so `declare -A`\nstops with an invalid-option error there. Name the shell the script really needs\nin the shebang, and test against the oldest version it has to run on.\n\n**A cleanup line deletes far more than it should.** With `build_dir` unset,\n`rm -rf \"$build_dir\"/*` expands to `rm -rf /*`. `set -u` turns an unset variable\ninto an error, and writing `\"${build_dir:?}\"` stops a non-interactive script\nwhenever the variable is unset or holds an empty string.\n\n## Further reading\n\n- [GNU Bash Reference Manual](https://www.gnu.org/software/bash/manual/bash.html)\n  — the 5.3 manual: expansions, quoting, the `set` builtin and the exact cases\n  where `set -e` is ignored.\n- [Shell Command Language](https://pubs.opengroup.org/onlinepubs/9799919799/utilities/V3_chap02.html)\n  — the portable `sh` language in POSIX.1-2024, the limit for any `#!/bin/sh`\n  script.\n- [Debian Policy: Scripts](https://www.debian.org/doc/debian-policy/ch-files.html#scripts)\n  — what `/bin/sh` must support on Debian, when a script must ask for Bash by\n  name, and the advice on `set -e`.\n- [Bash NEWS](https://tiswww.case.edu/php/chet/bash/NEWS) — the maintainer's\n  release-by-release list of new features, for checking which version a\n  construct needs.\n- [CVE-2014-6271](https://nvd.nist.gov/vuln/detail/CVE-2014-6271) — the NVD\n  record for Shellshock, with the attack paths it lists and the follow-up\n  identifier.\n\n\u003chttps://engineer.company/glossary/bash/\u003e\n","date_modified":"2026-11-09T21:50:25Z","description":"What Bash does, when a shell script is the right tool and when to switch to Python, and the quoting and error traps that make scripts report false success.","formats":{"gemtext":"https://engineer.company/glossary/bash/index.gmi","gopher":"https://engineer.company/glossary/bash/gophermap","html":"https://engineer.company/glossary/bash/","json":"https://engineer.company/glossary/bash/index.json","markdown":"https://engineer.company/glossary/bash/index.md","pdf":"https://engineer.company/glossary/bash/index.pdf"},"kind":"page","language":"en","title":"What is Bash?","url":"https://engineer.company/glossary/bash/","version":1}