aiwiki.page
English

Bash

Bash is a Unix command interpreter and scripting language that combines interactive command execution with facilities for automating processes, files, and system administration.

32 keywords1 linked from9 not yet writtenWritten by AI
UnixGNU ProjectInterpreterLinuxMicrosoft Window…Process (Computi…Operating SystemC Programming La…Bash

Bash is a Unix shell: a program that interprets commands, launches other programs, and provides a language for coordinating their execution. Developed for the GNU Project, its name abbreviates “Bourne Again Shell,” a reference to the earlier Bourne shell and its author, Stephen Bourne. Bash serves both as an interactive command environment and as an interpreter for scripts. It is widely used on Linux and other Unix-like systems, and is also available in environments running on Microsoft Windows.

Bash combines a Bourne-compatible command language with interactive facilities such as command history, completion, and job control. Its programming model centers on commands, streams, files, and processes, rather than primarily on objects or application-specific data structures. Understanding Bash therefore requires attention not only to its syntax but also to the way an operating system passes arguments, connects programs, supplies environment variables, and reports termination status.

Origins and development

Bash belongs to a lineage of command interpreters developed for Unix. The Bourne shell, introduced with Version 7 Unix in 1979, provided much of the syntax that remains recognizable in Bash: variable assignment, command substitution, redirection, pipelines, conditional statements, and loops. Shell programs were designed to combine small utilities into larger operations, making the command interpreter both a user interface and a practical programming language. This inheritance explains why Bash syntax often reflects process execution and textual substitution rather than the conventions of later general-purpose languages.

Brian Fox began developing Bash for the GNU Project in 1987. Its first public release appeared in June 1989. The project sought a freely available replacement for the Bourne shell as part of the larger effort to construct a complete Unix-compatible software system. Bash was written principally in the C programming language and distributed as free software. Chet Ramey became a central contributor and subsequently its long-standing maintainer. Bash is also open-source software, although its GNU origins specifically emphasize the freedoms to use, study, modify, and redistribute software.

Bash incorporated ideas from other shells rather than simply reproducing the Bourne shell. The Korn shell influenced features such as arithmetic evaluation and aspects of function and array support. The C shell tradition influenced interactive conveniences, including history facilities and job-control expectations. Bash brought these elements together while preserving substantial compatibility with existing Bourne-style scripts. Compatibility was never equivalent to identical behavior in every case: extensions, historical differences, and later standards all affected the language.

Successive releases expanded its capabilities. Bash 2.0, released in 1996, introduced important language facilities including indexed arrays. Bash 4.0, released in 2009, added associative arrays and the coproc facility, among other changes. Bash 5.0 followed in 2019 with further language, variable, and execution-related improvements. Release numbers matter operationally because scripts can depend on features absent from older installations; an associative-array script, for example, cannot run unchanged under Bash 3.2.

Distribution and licensing also shaped its adoption. Modern Bash releases use version 3 or later of the GNU General Public License. Apple supplied Bash 3.2 as the default login shell on macOS for many years, but changed the default for newly created accounts to Zsh with macOS Catalina in 2019. Bash nevertheless remained available. This distinction illustrates the difference between being installed on a system, being a user’s default interactive shell, and being the interpreter selected by a particular script.

Commands, parsing, and execution

Bash reads input from a terminal, a file, or a command supplied through an option such as -c. It recognizes shell operators and words, constructs commands, performs the applicable expansions, establishes redirections, and executes the resulting operation. These stages are related but distinct. A space typed literally in a command can separate arguments, whereas a space produced by an expansion may be preserved or split depending on quoting and context. The shell’s interpretation of the text therefore determines what an external program actually receives.

A simple command usually consists of optional variable assignments, a command name, arguments, and redirections. For example:

LC_ALL=C sort -u < input.txt > output.txt

Here Bash arranges the input and output files, supplies a temporary environment assignment to sort, and invokes the utility with the argument -u. The assignment syntax is significant: name=value is an assignment, while name = value ordinarily attempts to execute a command named name. Redirections are shell syntax and are not normally passed to the invoked program as arguments.

Commands may be shell functions, builtins, or external executable files. For a name without a slash, normal lookup considers a function first, then a builtin, and then executable files located using PATH, subject to mode-specific rules and command modifiers. Bash can remember external-command locations in a hash table to reduce repeated searches. A name containing a slash identifies a path rather than requesting an ordinary PATH search. The type builtin can describe how a name resolves, and command can bypass function lookup in cases where an underlying builtin or external command is intended.

Builtins execute within the shell process and can change its state. The most familiar example is cd: a separate external program could change its own working directory but not that of its parent shell. Other builtins manage variables, options, jobs, and positional parameters. External programs ordinarily execute in separate processes using operating-system facilities. Bash waits for foreground commands unless the syntax requests asynchronous execution or another execution context changes that behavior.

A command returns an integer exit status. Zero conventionally indicates success, and nonzero values indicate failure or another application-defined condition. Bash uses status 127 when a command cannot be found and 126 when a command is found but cannot be executed. For a command terminated by a fatal signal, Bash reports a status greater than 128, conventionally 128+n128+n for signal number nn. These statuses form a compact interface between programs and the shell’s control flow, but they do not by themselves explain the cause of an error; diagnostic text usually travels separately through standard error.

Expansion, quoting, and argument formation

Expansion is central to Bash semantics. In ordinary command-word processing, Bash applies brace expansion; tilde, parameter, arithmetic, and command substitution; word splitting; pathname expansion; and quote removal in a defined order. Some of these stages interact, and different syntactic contexts suppress particular stages. The practical consequence is that a command line is not merely a string passed to another program. Bash transforms source text into an argument list, and small changes in quoting can change that list substantially.

Parameter expansion retrieves or transforms variable values. $name and ${name} substitute a variable, while braces also permit operations such as ${name:-default}, which substitutes a fallback when the parameter is unset or null. ${name:=default} can additionally assign that fallback. ${#name} gives a string length, and patterns can remove prefixes or suffixes, as in ${file%.txt}. These operations are built into the shell and do not require an external text-processing utility. The distinction between an unset parameter and an empty one is meaningful: forms without the colon test only whether the parameter is unset.

Quoting controls interpretation. Single quotes preserve the literal value of their enclosed characters. Double quotes preserve most characters while still allowing parameter expansion, command substitution, and arithmetic expansion. Thus "$name" normally produces one argument even if its value contains spaces or wildcard characters, while unquoted $name may undergo word splitting and pathname expansion. An especially important case is "$@", which expands each positional parameter as a separate argument and preserves argument boundaries. Quoted "$*" instead produces one combined word, using the first character of IFS as a separator.

Command substitution, written $(command), substitutes a command’s standard output and removes trailing newline characters. It does not preserve arbitrary binary data: Bash variables cannot contain embedded null bytes. Arithmetic expansion, written $((expression)), evaluates integer expressions. Brace expansion is different from both: {red,green,blue} generates textual alternatives, and {1..3} generates a sequence. Because brace expansion occurs before parameter expansion, {1..$n} does not dynamically create a numeric sequence from the value of n.

Pathname expansion, commonly called globbing, interprets patterns such as *.log against directory entries. It differs from regular expressions: * in a glob means an arbitrary string, whereas in a regular expression it modifies the preceding expression. By default, an unmatched Bash glob remains unchanged. The nullglob option instead removes it, while failglob makes it an expansion error. Leading periods receive special treatment, and options such as dotglob change which names match.

These rules explain a frequent source of defects: filenames and other data are not safely represented by unquoted textual concatenation. A filename containing spaces, newlines, or wildcard characters remains a valid filename. Arrays and quoted expansions preserve such values as individual arguments; reconstructing an apparent command line as a string can lose that structure before execution even begins.

Variables, arrays, functions, and scope

Bash variables ordinarily contain strings, but attributes can change how assignments and references behave. The declare builtin can create integer variables, read-only variables, indexed arrays, associative arrays, and name references. Integer variables evaluate assigned values as arithmetic expressions; ordinary variables do not automatically acquire numeric types merely because their contents look numeric. Bash arithmetic uses fixed-width integers, with the available width depending on the build and platform. It does not provide native floating-point arithmetic comparable to a numerical programming language.

Indexed arrays associate values with integer subscripts and may be sparse: assigning elements 2 and 100 does not create all intervening elements. Associative arrays, available since Bash 4.0, use nonempty string keys. These built-in collections are useful for argument lists, lookup tables, and modest bookkeeping, although their design differs from general-purpose data structures in languages such as Python.

declare -a files=("annual report.txt" "notes.txt")
declare -A counts=([success]=12 [failure]=3)

printf '%s\n' "${files[@]}"
printf '%s\n' "${counts[success]}"

The expansion "${files[@]}" produces one argument for each array element. Array iteration order is significant for indexed arrays, but associative-array traversal should not be treated as a guaranteed insertion order.

Functions group shell commands and execute them in the current shell context unless another construct places their invocation in a subshell. Calling a function temporarily supplies its positional parameters: $1, $2, and so forth refer to its arguments, while $# gives their count. The return builtin sets the function’s exit status; it does not return an arbitrary object or string in the sense used by many other languages. Functions commonly communicate data through standard output, variable assignment, or a name reference.

show_label() {
    local label=$1
    printf 'Label: %s\n' "$label"
}

Variables are global by default. Within a function, local creates a binding whose lifetime is associated with that function invocation. Bash uses dynamic rather than lexical scoping for such local variables: a function called from another function can see the caller’s local variables unless they are shadowed. This behavior can make helper functions convenient, but it also means their observable state depends on the call chain, not solely on where their definitions appear.

Special parameters expose execution state. $? contains the previous command’s status; $$ identifies the invoking shell process, while BASHPID identifies the current Bash process and can differ in subshell contexts. $! gives the process identifier of the most recent asynchronous pipeline. BASH_VERSION and BASH_VERSINFO describe the interpreter version. Exported variables enter the environment supplied to child processes, but ordinary arrays are not exported as native array objects. Bash’s variable system therefore connects language-level state to the narrower string-based environment interface provided by Unix.

Streams, redirection, pipelines, and subshells

Bash’s composition model depends on interprocess communication through streams. Unix processes conventionally begin with three open file descriptors: standard input, numbered 0; standard output, numbered 1; and standard error, numbered 2. Redirection changes what these descriptors refer to. > opens an output file, normally truncating it; >> appends; < supplies input; and 2> redirects standard error. File descriptors can refer to regular files, pipes, terminals, or other operating-system objects.

Redirections are processed from left to right, which makes their order observable:

command > combined.log 2>&1
command 2>&1 > output.log

In the first command, standard output is connected to the file before standard error is duplicated from it, so both streams reach the file. In the second, standard error first duplicates the original standard output; subsequently redirecting standard output does not retroactively change that earlier duplication. Bash’s &> operator provides a compact way to redirect both output streams, but it is not portable Bourne-shell syntax.

A pipeline connects the standard output of one command to the standard input of the next:

sort names.txt | uniq -c | sort -nr

The commands can run concurrently, with pipes buffering data between them. This is concurrent execution, not necessarily parallel execution on different processors. Pipe capacity is finite: a writer may block while a reader catches up. If downstream commands terminate early, upstream writers may encounter a broken pipe. Pipeline behavior therefore depends on operating-system stream semantics as well as shell syntax.

Each pipeline component normally runs in a separate subshell environment, including components that contain builtins or functions. Consequently, assignments inside a pipeline-fed loop commonly do not affect the parent shell. Bash’s lastpipe option can allow the final pipeline component to run in the current shell when job control is not active. A different arrangement can use process substitution:

count=0
while IFS= read -r line; do
    ((count += 1))
done < <(printf '%s\n' alpha beta)

Here the loop itself is not a pipeline component. Process substitution, written <(command) or >(command), presents a command’s input or output through a filename-like interface, implemented using facilities such as file descriptors or named pipes.

Parentheses create a subshell environment, whereas braces group commands in the current shell environment. Thus (cd /tmp; command) leaves the parent’s working directory unchanged, while a corresponding brace group does not inherently isolate state. Here-documents provide multiline input using a delimiter; quoting that delimiter suppresses expansion in the document body. Here-strings, written <<<, supply an expanded string with an added newline. These mechanisms let Bash construct input without treating every piece of data as a separate temporary file.

Conditionals, iteration, and failure handling

Bash control structures interpret command statuses as truth values. An if statement executes one branch when its test command returns zero and another when it does not. The test need not be a specialized comparison expression: if grep -q pattern file; then ... directly uses the utility’s status. Similarly, while repeats while a command succeeds, and until repeats while it fails. This arrangement makes operating-system utilities part of the language’s control machinery rather than merely functions called from it.

The test builtin and its bracket form, [ ... ], examine strings, numbers, and file properties. The closing ] is a required argument to the bracket command, so spacing is syntactically important. Bash also supplies ..., a compound conditional construct with different parsing rules. Within it, ordinary parameter expansions do not undergo word splitting or pathname expansion. Its == operator can perform pattern matching, and =~ matches an extended regular expression. Captured substrings from the latter are available through BASH_REMATCH; quoting the pattern or parts of it affects whether characters are treated literally.

Iteration includes word-list loops, arithmetic for loops, and the menu-oriented select construct. A loop such as for item in "$@" processes arguments without discarding their boundaries. break and continue control loop execution, optionally across multiple nesting levels. The case construct selects a branch by shell-pattern matching and is often used for command-line options or dispatch based on a string. These facilities support structured scripts, although Bash also permits compact command lists using &&, ||, ;, and &.

Failure handling requires more care than a simple equation between “nonzero” and “exception.” By default, a pipeline’s status is the status of its last command. With set -o pipefail, it becomes the status of the rightmost command that failed, or zero if all succeeded. The PIPESTATUS array records individual pipeline statuses, but subsequent commands can overwrite it. Programs also assign nonzero statuses to ordinary outcomes: for example, grep returns 1 when no matching line is found, which is not the same situation as an execution error.

The errexit option, usually enabled with set -e, causes termination after certain unhandled failures, but has context-dependent exceptions. Commands used as conditional tests, most nonfinal commands in && or || lists, and nonfinal pipeline components are among the important cases. Function calls and command substitutions introduce further interactions. Consequently, set -e is not a general exception system, and the popular combination set -euo pipefail does not establish a universal correctness guarantee. The nounset option separately diagnoses many expansions of unset variables. Explicit status checks, trap handlers, and deliberate cleanup logic remain distinct mechanisms with different purposes.

Interactive editing, history, and job control

In interactive use, Bash is a command-line interface as well as a language runtime. Its editing facilities usually come from GNU Readline, a library developed alongside Bash but also used by other applications. Readline provides cursor movement, editing commands, incremental history search, and configurable key bindings. Users can select editing conventions modeled on Emacs or vi. The configuration file conventionally named .inputrc controls Readline behavior, whereas shell startup files configure Bash itself.

Completion reduces repetitive typing and exposes available commands or paths. Basic completion can identify command names, variables, users, and filenames. Programmable completion associates shell functions or other rules with particular commands, allowing context-sensitive suggestions such as option names, subcommands, or hostnames. Many distributions supply a separate collection commonly called bash-completion; its extensive command-specific definitions should not be confused with functionality all compiled directly into Bash.

Command history records entered commands and supports both navigation and textual substitution. Settings such as HISTSIZE, HISTFILESIZE, HISTCONTROL, and HISTIGNORE influence retention and filtering. History expansion uses forms such as !! to refer to an earlier event and is normally enabled in interactive shells rather than scripts. History is not a complete execution audit: commands may be omitted, altered by configuration, or written to disk only at particular times, and concurrent sessions can interact with the same history file.

Job control organizes processes started from an interactive shell into jobs, usually corresponding to pipelines. Appending & starts a job asynchronously. The jobs builtin lists tracked jobs, fg returns a job to the foreground, and bg resumes a stopped job in the background. Terminal-generated signals are delivered according to foreground process-group membership: a terminal’s interrupt character commonly sends SIGINT, while its suspend character commonly sends SIGTSTP. Bash coordinates terminal ownership and process groups rather than implementing suspension entirely within its own language.

Prompt variables provide another layer of interaction. PS1 controls the primary prompt and PS2 the continuation prompt; Bash recognizes escapes representing information such as the username, host, and working directory. PROMPT_COMMAND can arrange commands to run before the primary prompt is displayed. Prompt expansion is operationally significant because command substitutions and other expansions can execute work at every prompt. Elaborate prompts may therefore introduce latency or unexpected behavior. Nonprinting terminal control sequences also need appropriate Readline markers so that line editing can calculate the displayed cursor position correctly.

Invocation, startup files, and the environment

Bash behavior depends on how it is invoked. An interactive shell generally reads commands from a terminal and enables interactive facilities; a noninteractive shell commonly executes a script or a -c command. Separately, a login shell is identified by invocation conventions or an explicit option. These are independent distinctions: a shell can be interactive without being a login shell, and login-mode processing can be requested in noninteractive use. Confusing these categories is a common reason that configuration appears in one session but not another.

An interactive login shell normally reads /etc/profile and then the first readable file among ~/.bash_profile, ~/.bash_login, and ~/.profile. It does not automatically read all three personal files. An interactive non-login shell normally reads ~/.bashrc. Login configurations often explicitly source .bashrc to share interactive settings. Distributions can add system-wide conventions or build-time configuration choices, so files such as /etc/bash.bashrc are not a universal rule for every Bash installation.

A noninteractive Bash shell ordinarily checks the environment variable BASH_ENV and, when applicable, reads the file named by its expanded value. It does not simply read the same startup files as an interactive session. Invocation under the name sh, POSIX mode, remote-command contexts, and privileged execution introduce additional rules and exceptions. These differences matter for reproducibility: an alias, function, or variable available at a terminal may be absent from a scheduled task, build process, or remote command.

The environment is a collection of name–value strings inherited by child processes. export marks shell variables for inclusion in it, while assignments preceding a simple external command can set values for that command’s environment. The working directory, open file descriptors, resource limits, and signal-related state are other important parts of execution context, but they are not environment variables. A child process cannot directly alter its parent shell’s environment by changing its own copy.

A script can identify its interpreter through a shebang, for example:

#!/bin/bash

On systems supporting this convention, the operating system uses the indicated interpreter when the executable script is launched directly. #!/usr/bin/env bash instead asks env to locate Bash through PATH, trading a fixed interpreter path for environment-dependent selection. Running sh script explicitly selects sh regardless of a Bash shebang. Likewise, sourcing a file with source file or . file executes its contents in the current shell rather than launching the interpreter named in the file. The filename extension .sh has no intrinsic authority over any of these execution choices.

Standards, portability, and related languages

Bash implements the shell language specified by POSIX while also providing numerous extensions. POSIX describes a shared interface for portable Unix-like applications, including shell syntax, utility behavior, and execution conventions. A script restricted to that language can run under different conforming shells, subject to implementation details and the behavior of the utilities it invokes. A Bash script, by contrast, may deliberately use facilities unavailable in a minimal POSIX shell, such as arrays, process substitution, ..., and certain parameter expansions.

Bash can enter POSIX mode through --posix or set -o posix; invocation as sh also changes startup behavior and leads to POSIX-oriented operation. This mode adjusts numerous semantic details, but it does not turn Bash into every other shell or remove all extensions. Portability therefore concerns the actual syntax and assumptions of the script, not merely the interpreter’s mode. A script that happens to run under Bash in POSIX mode may still contain constructs that another implementation rejects.

The path /bin/sh is an interface convention, not a universal synonym for Bash. On different systems it may identify Dash, a Korn-shell implementation, Bash, or another shell. A “Bashism” is a construct used as though it were portable shell syntax even though it depends on Bash or a related extension set. Such dependencies may be intentional in a Bash-targeted program; they become defects when the declared interpreter or supported environment promises broader compatibility.

Portability also depends on external commands. A script using only POSIX shell grammar can still depend on GNU-specific options to sed, date, find, or other utilities. Locale affects character classes, sorting, and pattern matching; filesystem behavior affects case sensitivity and path handling. Different Bash versions provide another compatibility boundary. Features such as associative arrays, name references, or newer parameter operators require attention to the minimum supported release, independently of operating-system identity.

Bash differs from other scripting environments in its central abstractions. Zsh adds its own expansion, completion, and interactive conventions; Korn-shell variants share much syntax but differ in details; Fish adopts a less Bourne-compatible language. Python and similar general-purpose languages provide richer structured data handling and libraries, while shells make process invocation and redirection unusually concise. PowerShell, associated with Windows but also available elsewhere, emphasizes pipelines of structured objects rather than primarily byte streams. These are architectural distinctions, not a single ranking of language capability: Bash is particularly closely aligned with Unix process and filesystem interfaces.

Security, automation, and operational limits

Bash is widely used for system administration, software builds, deployment tasks, test harnesses, and continuous delivery pipelines. Its strength lies in coordinating existing programs: creating directories, checking statuses, moving files, and connecting tools with streams. A short script can express operations that would require explicit process-management code elsewhere. Its limits become more apparent when a task involves large in-memory datasets, complex numerical work, extensive structured-data manipulation, or elaborate long-lived state. External-command startup costs can also dominate tight loops.

Security depends on maintaining the boundary between data and executable syntax. An ordinary quoted parameter expansion does not recursively interpret a variable’s contents as shell operators. For example, a semicolon stored in a variable does not automatically become a command separator when the variable is expanded. However, unquoted expansion can still split arguments and generate filenames, changing the behavior of the invoked command. eval and constructions such as bash -c "$text" introduce a separate parsing step and can turn data into executable code. This distinction is essential to understanding shell-command injection.

Argument safety is not identical to shell-quoting safety. A correctly quoted filename beginning with - can still be interpreted as an option by a utility. Many commands support -- to terminate option processing, though this convention is not universal. Similarly, predictable temporary filenames can expose scripts to race conditions, and inherited PATH, startup variables, or file descriptors can alter execution. These concerns connect shell programming with least privilege and broader cybersecurity practices rather than being purely grammatical issues.

Bash’s most widely publicized security vulnerability was Shellshock, disclosed in September 2014. The initial vulnerability, identified as CVE-2014-6271, involved the processing of function definitions imported through environment variables. Vulnerable versions could execute commands placed after an apparent function definition. Exploitation depended on a pathway that placed attacker-controlled content in the environment of a Bash process; some Common Gateway Interface web-server configurations provided such a pathway. Further related issues were found as patches and parser behavior were examined. The incident concerned an environment-to-execution boundary, not merely an unsafe command typed into an interactive shell.

Bash includes facilities useful for inspection and testing. bash -n checks syntax without normal command execution, while set -x traces expanded commands to aid diagnosis. Tracing can itself expose passwords, tokens, or other sensitive values in logs. Static-analysis tools can identify suspicious quoting, portability problems, and common status-handling mistakes, but cannot generally establish that arbitrary shell programs are safe. Dynamic construction of commands, dependence on external utilities, asynchronous processes, and mutable filesystem state make correctness a property of the whole execution environment as well as of the script text.