Bash (Unix shell)

From Vero - Wikipedia
(Redirected from Bourne-Again shell)
Jump to navigation Jump to search

Template:Short description Template:Multiple issues Template:Use dmy dates Template:Infobox software

Bash (short for "Bourne Again SHell") is an interactive command interpreter and programming language developed for Unix-like operating systems. Created in 1989 by Brian Fox for the GNU Project,<ref name="computerworld_08"> Template:Multiref2 </ref> it is designed as a completely free software alternative for the Bourne shell, Template:Code, and other proprietary Unix shells,<ref>https://www.gnu.org/software/bash/</ref> supported by the Free Software Foundation.<ref name="computerworld_08" /> Having gained widespread adoption, Bash is commonly used as the default login shell for numerous Linux distributions.<ref name="goftw2015"> Template:Cite web </ref> It also supports the execution of commands from files, known as shell scripts, facilitating automation.

The Bash command syntax is a superset of the Bourne shell's syntax, from which all basic features of the Bash syntax were copied. As a result, Bash can execute the vast majority of Bourne shell scripts without modification. Some other ideas were borrowed from the C shell, its successor tcsh, and the Korn Shell. It is available on nearly all modern operating systems, making it a versatile tool in various computing environments.

Definitions

ASCII, strings and numbers

Template:Blockquote

<syntaxhighlight lang="console">$ printf '<newline>: <%b>\n' $'\n' <newline>: < > $ printf '<tab>: <%b>\n' $'\t' <tab>: < > $ printf '<space>: <%s>\n' " " <space>: < > $ printf '<NUL>: <%b>\n' $'\0' <NUL>: <> </syntaxhighlight>

Any series of characters is called a "string", or sometimes a "string literal". In Unix-like operating systems, all characters, printable and non-printing, except for a few such as the null character and forward slash Template:Char, can be used in filenames. In addition, all strings are case-sensitive.<ref> Template:Cite book </ref>

Bash, like many other programming languages, uses zero-based numbering.

Control+key combinations

The Control+key functionality is provided by GNU Readline and is available in interactive mode only. Certain keypress combinations allow a user to operate Bash to use tab completion and to search the command history.

Some keypress combinations also allow a user to operate the terminal emulator in order to move the cursor within the terminal window and to control the emulator program. By default, these keypress combinations in Bash mirror those of Emacs.<ref> Template:Cite web </ref>

Default keybindings for control codes include:

Vi keybindings are also available and can be enabled by running Template:Code.<ref> Template:Cite web</ref><ref> Template:Cite web</ref>

Syntax

When Bash reads a full command line, the complete string is broken down into tokens. "Tokens" are identified using, and separated from each other using metacharacters.

As of Bash 5.3, the 10 metacharacters are the space, tab, and newline, as well as the following characters: Template:Code

"Blanks" are composed entirely of unquoted metacharacters, "operators" each contain at least one unquoted metacharacter and "words" may not include any unquoted metacharacters.

In practice, Bash breaks down full command strings into tokens or groups of tokens that do contain metacharacters and tokens or groups of tokens that do not contain any metacharacters—called "words". From there it further breaks words down into more specific, meaningful pieces like command names, variable assignment statements, etc.

The two blanks are space and tab.

Operators

Control operators perform a control function. They can be either a newline or one of the following: ||, &&, &, ;, ;;, ;&, ;;&, |, |&, (, or ).

Redirection operators redirect the input or output streams. They include <, >, &>, <<, and <<<.

Words

A word is a sequence of (non-meta-) characters treated as a single unit by the shell. A reserved word is a kind of a word that has a special meaning to the shell.<ref> Template:Cite web </ref> A name is a kind of a word separate from reserved words. Names consist solely of letters, underscores and numbers; which begins with either a letter or an underscore; which, however, may not begin with a number. Names also called identifiers, may be used for naming variables and functions.

Sixteen of the twenty-two "reserved words", which may be characters or words are as follows:

<syntaxhighlight lang="bash"> '!' '[[' '{' ']]' '}' case in esac for do done if then elif else fi ... </syntaxhighlight>

Names may only contain the characters ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789_.

In the following example of a full command string, metacharacters have a comma placed above them, Template:Code, reserved words have a caret placed beneath them, Template:Code, and other tokens have a backtick placed also beneath them, Template:Code.

<syntaxhighlight lang="console"> $ #, , , ,, , ,, , $ if echo foo; then bar=abc; fi $ # ^^ ```` ``` ^^^^ ``````` ^^ </syntaxhighlight>

Subshells

A "subshell" is an additional instance of the shell which has been initialized by a current instance of the shell. When a "parent" shell creates a subshell, or a "child" shell, an exact copy of the parent's environment information is re-created and becomes the environment of the subshell.

In Bash, in non-arithmetic contexts, one can force the use of a subshell by enclosing a full command string in single parentheses.

<syntaxhighlight lang="console"> $ echo foo foo $ ( echo foo ) foo $ </syntaxhighlight>

For this simple case, the preceding two commands are equivalent, however, use of subshells can have certain unexpected side effects. There are numerous different forms of syntax which can cause the initialization of a subshell.Template:Clarify

Expansion

Template:Main

Data structures

Bash offers variables and arrays as data structures, and though there are numerous kinds of each of these available, the data structures are relatively simple compared to other languages like C or Java.<ref name="gnu manual" /> All data is stored in memory as a string.

Beginning a word with a dollar character signifies that the word is the name of a variable or array. Surrounding the dollar / variable name syntax in double quotes is always advised. This practice shields the value(s) held by the parameter(s) from unwanted side effects.Template:Clarify

Wrapping the variable name in curly brackets (braces) Template:Code is recommended for readability and consistency between variables and arrays. When writing variables, curly brackets are optional and square brackets would be a syntax error. The parameter names are always on the left side of the equals sign and values are always on the right.

Variables

A variable is assigned to using the syntax name=value.

To use a variable, the syntax $name is used, or ${name}, which expands to the value assigned to the variable.

The latter syntax must be used for certain names to prevent unwanted side effects. For example, $10 will be parsed as ${1}0, so using ${10} means it will be parsed as intended.

Positional parameters, usually passed to a bash script, are denoted by the variables numbered starting from $0. Special parameters are signified by punctuation characters.<ref name="gnu manual" /> For example, Template:Code expands to a list of the first through last positional parameters, "individually requoted, separated by spaces".Template:Attribution needed

Environment variables are signified by all capital letters. Environment variables include UNIX variables like Template:Code, and Bourne shell variables such as Template:Code.<ref name="gnu manual" /> Scripting variables are signified by all lower case letters or CamelCase. This is only convention; any variable can be passed to the Template:Code command to be made into an environment variable.

Arrays

Arrays are data structures which hold multiple values.<ref>https://www.gnu.org/software/bash/manual/html_node/Arrays.html</ref> Arrays have a set of square brackets placed at the end of the variable name and inside the curly braces. When writing arrays, curly braces and square brackets are required.

An array is assigned using the syntax name=( one or more elements ). It is expanded using Template:Code or Template:Code or Template:Code, depending on the use case.

Each kind of parameter is distinguished by a specific naming convention.<ref name="gnu manual">https://www.gnu.org/software/bash/manual/bash.html</ref>

Since Bash 4.0,<ref>https://www.linux-magazine.com/Online/News/Bash-4.0-Introduces-Associative-Arrays</ref> Bash also supports associative arrays.

In this article, examples of variables from this section include Template:Mono, Template:Mono and Template:Mono.

Execution

Template:Main

"Execution" of a given program occurs when a user (or some other program) asks the operating system to act upon the instructions contained in the given program.

By default, Bash reads user code one line at a time, interprets any newline or semi-colon character Template:Code as the end of the current command, and executes commands in sequence. If an interactive command extends beyond the width of the terminal emulator, it is usually possible to keep typing and the command will wrap around. To extend a command beyond a newline onto an additional line, it is necessary that the final character of the first line be an unescaped backslash, Template:Code, which signals "line continuation". Bash always finishes parsing and executing one full commandline before moving on to and beginning with the parsing of the next commandline.

<syntaxhighlight lang="console"> $ foo=aa bar=bb quux=cc zork=dd; set -o xtrace $ : "${foo}"; : "${bar}" + : aa + : bb $ : "${quux}" \ > : "${zork}" + : cc : dd $ </syntaxhighlight>

The first word of a command line is known as the "command position". Under UNIX coventionality, the first word of the command line is always some kind of command, and the rest of the words in the command line string are either options for the command, arguments for the options, or some kind of input upon which the command will operate. "Options" are also called "flags", "switches", or, more formally, "operators". When Bash attempts to locate a command for execution, the directories it searches are those listed in the Template:Code variable and the current working directory.<ref name="tldp_3.2.1"> Template:Cite book</ref>

<syntaxhighlight lang="console"> $ # [COMMAND POSITION] [OPTION] [ARGUMENTS] $ # ,--^ ,------------^ ,----^ $ declare -p USER BASH_VERSION declare -x USER="liveuser" declare -- BASH_VERSION="5.2.37(1)-release" $ </syntaxhighlight>

Users and PS1

A user account can be created for either a human or a programmatic user. In Unix-like operating systems, there are two kinds of users: "privileged" and "regular". A privileged user, such as root or the operating system kernel, is allowed to do anything whatsoever on the machine. Unprivileged users are limited in various ways.

When an interactive shell session waits for user input, by default it prints a particular string of characters to the screen. In Bash, the value of this waiting-string is held in the shell variable Template:Code. For regular users, a common default value for Template:Code is the dollar character, Template:Char.Template:Efn For the superuser, a common default value is hashtag (Template:Char)

<syntaxhighlight lang="console"> $ sudo --login --user root [sudo] password for liveuser:

  1. vim /home/liveuser/names.txt
  2. exit

$ grep -e bob ./names.txt grep: ./names.txt: Permission denied </syntaxhighlight>

Modes

Programming paradigm

Template:Blockquote

Bash is written in C. A modular style can be approximated through good style and careful design.<ref> Template:Cite web</ref> It is often used in an imperative or procedural style.

Interactive and non-interactive modes

As a command processor, Bash can operate in two modes: interactive or non-interactive. In interactive mode, commands are usually read from a terminal emulator. In non-interactive mode, which facilitates automation, commands are usually read from named files known today as shell scripts. When executed as a standalone command at the command-line interface (CLI), by default Bash opens a new shell in interactive mode.

Scripts

Shell scripts are text files that contain code, often commands, intended to be read and acted upon by some particular interpreter in a batch process in a non-interactive mode and without any further user interaction. Interpreted scripts are programs that do not require their source code to be compiled: all of the relevant source code is contained within the script. There are many programs which can serve as a script interpreter: Perl, AWK, etc. Interpreted scripts are most often written for Unix shells.

The first two characters of the first line of any (executable) shell script begins with a something called a shebang: literally the characters hashtag (Template:Char) and bang (Template:Char) side by side.

<syntaxhighlight lang=console> $ cat ./example.sh

  1. ! /bin/env bash

echo foo exit

$ </syntaxhighlight>

If a script is intended to be run by a user as a stand-alone program on the commandline, then it is referred to as an "executable". By convention, the filenames of executable unix shell scripts are identified the suffix Template:Code. The "execute" bit can be enabled on a shell script with the utility Template:Code:

<syntaxhighlight lang=console> $ ls -l ./example.sh -rw-r--r--.1 liveuser liveuser 32 Aug 3 22:33 example.sh $ ./example.sh bash: ./example.sh: Permission denied $ chmod 0744 ./example.sh $ ls -l ./example.sh -rwxr--r--.1 liveuser liveuser 32 Aug 3 22:33 example.sh $ ./example.sh foo $ </syntaxhighlight>

The Template:Code builtin

With the Template:Code, or synonymous Template:Code command, Bash reads and executes shell commands from any text file by name.<ref> Template:Cite web </ref>

Login and non-login shells

Template:Main

Bash can be executed as a login shell, or "session leader," in both interactive and non-interactive modes via the Template:Code option. "Logging in" requires user authentication. For this reason, only one login shell exists per user session. In GNU/Linux, a user's login shell is identified in the /etc/passwd file.

<syntaxhighlight lang="console"> $ awk -F ':' '$1 ~ /root/' /etc/passwd root:x:0:0:Super User:/root:/bin/bash </syntaxhighlight>

When a human user initiates a login session, this procedure often occurs in a graphical user interface (GUI). When a user opens a terminal emulator, the emulator executes a non-login instance of the user's login shell.

Logging out of a shell session from within a terminal emulator can be accomplished with the Template:Mono command or, by default in Bash, pressing Template:Keypress.

Startup Template:Code files

Template:Main

When Bash starts, it uses Template:Code to execute commands in a variety of dotfiles (see lists below).<ref name="drdobbs"> Template:Cite web</ref> These dotfiles, unlike shell scripts, typically have neither the execute permission enabled nor a hash-bang. By default Bash will source a somewhat different set of files, and in a different sequence, depending on:<ref> Template:Cite web</ref>

Of course, any startup file can also execute commands from any other file. Startup files can affect shell behavior, terminal emulators, the X window system and the window manager.

POSIX mode

Template:Main

The POSIX IEEE 1003.1 standard specifies a common set of definitions that any shell system application (bash, dash, zsh, etc.) may conform to. Any shell user script (Template:Code) written in conformance with POSIX guidelines should be executable by any shell system application that has implemented the POSIX specification. As a result, there can be a reasonable expectation that POSIX-compliant scripts can be executed with success on any Unix or Unix-like operating systems which implements the POSIX standard (Linux, OpenBSD, Oracle Linux, HP-UX, etc.). These scripts are considered "portable" as they are and without any further modifications. The portion of POSIX that applies to shells and command line utilities is a subset of a larger group of POSIX standards that further specify how terminals and terminal emulators aught to function in order to also be considered portable.

When Bash is operating in POSIX mode, fewer features are available but the resulting code can be executed on a greater variety of operating systems.

To enable POSIX mode at the initialization of an interactive shell, Bash can be executed as either Template:Code, Template:Code or Template:Code.<ref name="tldp_psx"> Template:Cite book</ref> To cause a script to be initialized in POSIX mode, one would use the either the hashbang Template:Code or the less portable Template:Code. When an instance of Bash is operating in POSIX mode, the environment variable Template:Code is defined, and the value of the environment variable Template:Code includes the string Template:Mono.

<syntaxhighlight lang="console"> $ declare -p POSIXLY_CORRECT bash: declare: POSIXLY_CORRECT: not found $ sh $ declare -p POSIXLY_CORRECT declare -- POSIXLY_CORRECT="y" $ </syntaxhighlight>

The full list of features available in Bash which are not specified by POSIX is considerable.<ref> Template:Cite web</ref> Here is a partial list:

  • Any arrays other than the array of positional parameters, Template:Code, are not POSIX
  • The double bracket extended test construct, Template:Code, is not POSIX
  • One of the double-parentheses arithmetic-evaluation syntaxes, Template:Code, is not POSIX
  • Brace expansion, kernel{,-headers}, is not POSIX
  • Dynamic scoping of parameters and the Template:Code builtin are not POSIX
  • Process substitution, Template:Code, is not POSIX
  • Certain string-manipulation operations in parameter expansions are not POSIX
  • Most Bash builtin commands are not POSIX
    • The command Template:Code prints the list of Bourne special builtins, which are POSIX<syntaxhighlight lang="console">

$ enable -s | wc --lines 16 $ enable | wc --lines 61 </syntaxhighlight>

System commands which are available in modern Unix-like operating systems, and which are also specified by POSIX, may have fewer option flags or fewer relevant environment variables available under POSIX. Most (such as Template:Code) are standalone programs in the Template:Code, Template:Code, Template:Code or Template:Code directories (in Linux, typically provided by GNU coreutils or BusyBox) rather than Bash builtins.

Because of these and other differences, modern (version 5) Bash shell scripts are rarely runnable "as-is" under the Bourne or legacy Korn shell interpreters. Scripting with portability in mind is becoming less common as GNU/Linux becomes more widespread.<ref name =tldp_psx/><ref name=deb_pol> Template:Cite book</ref>

Code that is valid syntax in Bash but not specified by POSIX is called a "bashism". The program Template:Code can be used to make sure that a script can be executed in Debian Linux without any portability errors.<ref name="checkbashisms-1"> Template:Man</ref> Vidar Holen's Template:Code is another static linter written in Haskell which can parse script syntax for compatibility with any or all of bash, dash, ksh, and Bourne sh.<ref name="shellcheck-1">Template:Man</ref>

The syntax requirements for each shell are each a little different. For example, Debian's policy allows some extensions in their scripts (as they are in the dash shell),<ref name =deb_pol/> while a script intending to support pre-POSIX Bourne shells, like autoconf's Template:Mono, are even more limited in the features they can use.<ref> Template:Cite book</ref>

Other modes

Restricted mode

A restricted shell is used to set up an environment more controlled than the standard shell. A restricted shell behaves identically to bash with the exception that numerous actions are disallowed or not performed, including:

Once restricted mode is enabled, it cannot be disabled. These restrictions are enforced after any startup files are read, and it does not apply to shell scripts. Restricted mode is rarely used.

Privileged mode

In Bash, "privileged mode" is a rarely used option inheritedTemplate:Citation needed from the SVR4.2 UNIX System V shell (circa 1992).<ref>Template:Cite book</ref> It can be enabled with Template:Code and disabled with Template:Code.<ref> Template:Cite book</ref> When privileged mode is enabled, the Template:Code shell variables includes the string "privileged".

Extended debugging mode

Enabled via Template:Code at invocation or via Template:Code during either interactive or non-interactive modes. It uses a separate program called Template:Mono.<ref name="ref_bashdb"> Template:Cite web</ref> Template:Mono is not available in POSIX mode. See documentation for more information. See also Template:Section link.

Compatibility modes

Template:Blockquote

Observability

The Template:Mono option

When Template:Mono is enabled, simple debugging content is printed to the terminal. It can be enabled with Template:Code or Template:Code, and disabled with Template:Code, Template:Code or Template:Code. These options are also accepted at the commandline and at hash-bangs: Template:Code, etc.

<syntaxhighlight lang="console"> $ bash -x $ echo $(( 2 + 2 )) + echo 4 4 $ set -- 1 2 3 $ printf '<%s>\n' "$@" + printf '<%s>\n' 1 2 3 <1> <2> <3> $ </syntaxhighlight>

The Template:Mono shell setting is specified by POSIX. See also Template:Section link.

The Template:Mono option

The verbose option prints strings to the terminal as they are read, and before any expansions are performed. Rarely used.<ref>See Template:Code in the documentation.</ref>

Comments

Comments can be a valuable way of clarifying information or explaining a script or source file to someone else who might not be familiar with the scripter's intentions or context.

Standard comments in Bash are denoted with a hash character: Template:Char. Any text to the right of the hash to the end of the line will be ignored. Inline comments are allowed, but hash comments will not print during debugging. See also: Template:Section link.

Comments denoted with a colon character, Template:Char, originated with the Thompson shell. Any arguments to the right of colon Template:Char builtin are ignored. Inline comments are not possible, but colon comments will print during debugging and any parameters will have been expanded. <ref> Template:Cite web</ref>

<syntaxhighlight lang="console">

$ # Define foo $ foo=bar # An inline hash comment occurs on the same line as a command $ set -x $ # A regular comment (no output) $ : "${foo}" + : bar $ </syntaxhighlight>

Exit codes

When bash executes commands, exit status codes, also called "return codes," are produced which can offer some insight into the manner in which a program ceased running. The value of the most recently captured exit code is held within the shell parameter, 'question mark:' Template:Code. In non-arithmetic contexts, (i.e., most of the time) the numerical or "Boolean" value of "true" is zero (0), and the value of "false" is one (1).

When a system command has executed, the intended meaning of its exit status can most often be found in its man page; usually a zero indicates success and a nonzero exit status indicates some kind of failure condition or partial success. Template:Code is a well known command with three meaningful exit codes: 0, 1, and 2.

In Bash, within arithmetic contexts, the numerical truth values are reversed: "true" is one and "false" is zero. An arithmetic context can usually be identified by the syntax Template:Code or Template:Code. If an arithmetic statement evaluates to the integer zero, then the statement is considered "true," and the exit code is one. If the statement evaluates to any number other than zero the arithmetic statement is "false" and the exit code is zero.

Not all Linux/UNIX commands provide meaningful exit codes beyond zero and one, and there is no standard system for definitions of exit codes in Linux.

<syntaxhighlight lang="console">

$ true; echo "$?" # Exit code means "true" 0 $ false; echo "$?"; echo # Exit code means "false" 1 $ $ bash -c 'exit 99'; printf 'exit-code: %d\n\n' "$?" exit-code: 99 $ $ (( 1 - 1 )); printf '%d\n' "$?" # This exit code means "true" 1 $ (( 1 + 1 )); printf '%d\n' "$?" # ...and this exit code means "false" 0 </syntaxhighlight>

Job control

Template:Main

The Bash shell has two modes of execution for commands: batch (asynchronous), and concurrent (synchronous). To execute commands in batch mode (i.e., in sequence) they must be separated by the character Template:Char, or on separate lines:

<syntaxhighlight lang="console">

$ command1; command2 $ command3 $ </syntaxhighlight>

In this example, when Template:Mono is finished, Template:Mono is executed, and when Template:Mono has completed, Template:Mono will execute. A background execution of Template:Mono can occur using symbol Template:Char at the end of an execution command, and process will be executed in background while immediately returning control to the shell and allowing continued execution of commands.

<syntaxhighlight lang="console">

$ command1 & $ </syntaxhighlight>

Or to have a concurrent execution of Template:Mono and Template:Mono, they must be executed in the Bash shell in the following way:

<syntaxhighlight lang="console">

$ command1 & command2 $ </syntaxhighlight>

In this case Template:Mono is executed in the background, Template:Char symbol, returning immediate control to the shell that executes Template:Mono in the foreground. A process can be stopped and control returned to bash by typing Template:Key press while the process is running in the foreground.<ref> Template:Cite web</ref> A list of all processes, both in the background and stopped, can be achieved by running Template:Mono:

<syntaxhighlight lang="console">

$ jobs [1]- Running command1 & $ </syntaxhighlight>

In the output, the number in brackets refers to the job id. The plus sign signifies the default process for Template:Mono and Template:Mono. The text "Running" and "Stopped" refer to the process state. The last string is the command that started the process.

The state of a process can be changed using various commands. The Template:Mono command brings a process to the foreground, while Template:Mono sets a stopped process running in the background. Template:Mono and Template:Mono can take a job id as their first argument, to specify the process to act on. Without one, they use the default process, identified by a plus sign in the output of Template:Mono. The Template:Mono command can be used to end a process prematurely, by sending it a signal. The job id must be specified after a percent sign:

<syntaxhighlight lang="console">

$ sleep 100 & [1] 4904 $ kill %1 $ jobs [1]+ Terminated sleep 100 $ </syntaxhighlight>

Job control, also known as "Monitor mode," is enabled by default in interactive shells, and can be disabled with Template:Code.

Signals

Signaling is a means of inter-process communication (IPC). Sometimes a commandline process may seem to freeze in the middle of execution. In these instances it may become necessary to identify which process may be blocked and to manually end the offending process.

At an interactive terminal, it is usually sufficient to press Template:Key to end the current foreground process and return control back to the user prompt, or to press Template:Key to suspend it. Occasionally attempting to suspend a process will succeed when attempts to cancel a process appear unresponsive. In other cases it may be necessary to use the Template:Mono program to send an IPC signal. In this example, we use the Template:Mono command from a second terminal screen to terminate the process with PID 4331.

<syntaxhighlight lang="console">

$ tty # Terminal one /dev/pts/0 $ whoami liveuser $ sleep 1000 # Command hangs </syntaxhighlight>

<syntaxhighlight lang="console">

$ tty # Terminal two /dev/pts/1 $ whoami liveuser $ ps aux | grep -e sleep -e PID USER PID %CPU %MEM VSZ RSS TTY STAT START TIME COMMAND liveuser 4331 0.0 0.0 230336 2312 pts/1 S+ 11:19 0:00 sleep 1000 liveuser 4333 0.0 0.0 231248 2516 pts/0 S+ 11:19 0:00 grep --color=auto -e sleep -e PID $ kill 4331 $ ps aux | grep -e sleep -e PID # The sleep process has ended USER PID %CPU %MEM VSZ RSS TTY STAT START TIME COMMAND liveuser 4333 0.0 0.0 231248 2516 pts/0 S+ 11:19 0:00 grep --color=auto -e sleep -e PID $ </syntaxhighlight>

<syntaxhighlight lang="console">

$ tty # Terminal one again /dev/pts/0 $ whoami liveuser $ sleep 1000 Terminated $ </syntaxhighlight>

In Unix-like operating systems, a user is allowed to instruct the kernel to send a signal to a process that is owned by the user. A regular user may not send a signal to a privileged process. Signals can be sent to a process using the Template:Mono builtin or using the system binary of the same name.

<syntaxhighlight lang="console">

$ whoami liveuser $ ps aux | awk '$2 ~ /\<1\>/' # Let\s view some info on the kernel process, process 1. root 1 0.0 0.2 37140 20440 ? Ss 04:44 0:18 /usr/lib/systemd/systemd --switched-root --system --deserialize=53 rhgb $ kill -s SIGKILL 1 bash: kill: (1) - Operation not permitted $ type -a kill kill is a shell builtin kill is /usr/bin/kill $ /usr/bin/kill -s SIGKILL 1 kill: sending signal to 1 failed: Operation not permitted $ </syntaxhighlight>

The most commonly used signals can be viewed with kill -L | head -n 4. Each IPC signal is associated with a signal number, but exit codes and signal codes are two different things. While sending a process an IPC signal of 9 (a "KILL" signal) will almost certainly terminate the process immediately, it will most likely not result in the process returning an exit code of 9.

By default in Bash, builtin kill sends a TERM ("terminate") signal. It's common for commandline utilities to respond to a SIGTERM by shutting down and exiting cleanly. (TERM and SIGTERM are the same, the SIG- prefix to all signal names can be omitted.) The Ctrl-c keypress sequence in Bash sends a SIGINT, interrupt signal, to the foreground process. The Ctrl-z keypress sequence sends the SIGSTOP, stop signal.<ref> Template:Cite book</ref> When a process receives a SIGKILL, the process terminates immediately and messily. It is recommended to use SIGKILL only as a last resort.<ref> Template:Cite book</ref> The SIGKILL signal cannot be blocked or handled.

Processes can "catch" and "handle" IPC signals they receive. A user can use the kill builtin to "send" an IPC signal to another process. That target process can set up a mechanism, some plan beforehand, for how to repsond whenever any particular signal might be received, or "caught." The way a target program responds is referred to as how the program "handles" receiving the signal. In the man pages one can see how some system commands will print out certain information to the terminal when they receive a SIGHUP: for example, the Template:Code command.<ref>Template:Cite web</ref>

Template:Blockquote

By default Bash shell scripts receive and respond to any and all IPC signals sent to them, however, Bash scripts can utilize the Template:Mono builtin to catch and handle signals.<ref> Template:Cite web</ref>

<syntaxhighlight lang="console">

$ cat ./trap-example.sh

  1. ! /usr/bin/env bash

trap umask EXIT echo bar exit 0 $ chmod 0700 trap-example.sh $ ./trap-example.sh bar 0077 $ </syntaxhighlight>

There are a few signals which are only available from within Bash as GNU extensions: Template:Mono, Template:Mono, Template:Mono and Template:Mono. These signals can be useful in debugging, and can only be sent and handled by shell builtins. See also Template:Section link.

Values of parameters

There are many different implementations of Template:Mono. Some have the Template:Code option, and some don't.<ref> Template:Cite book</ref> The list of options is not uniform across implementations, though Template:Mono and Template:Mono are both specified by POSIX. If a scripter wishes to know the precise value of a string contained by a variable, then the most consistent way of doing so is to use Template:Mono.

For any string containing any character (besides null?) including digits, the format specifier is Template:Mono.Template:Citation needed

<syntaxhighlight lang="console">

$ foo=abc bar=123 $ printf '<%s>\n' "${foo}" "${bar}" <abc> <123> $ </syntaxhighlight>

For digits only, the format specifier is Template:Mono.

<syntaxhighlight lang="console">

$ printf '<%d>\n' "${foo}" "${bar}" bash: printf: abc: invalid number <0> <123> $ </syntaxhighlight>

With Template:Mono, a newline is never included in the output unless the scripter includes a newline in the format string. In the example below, where a newline has been omitted from the format string, the value of PS1 is printed on the same line as the output of the previous command.

<syntaxhighlight lang="console">

$ printf '<%s>' "${foo}" "${bar}" <abc><123>$ </syntaxhighlight>

Another very consistent method is to use Template:Code. The output of Template:Code can be reused as input. However, not all variables and parameters can be printed using Template:Code, for example, the values of the Special Parameters. The Special Parameter hashtag, Template:Code, reports how many Positional Parameters are currently defined.

<syntaxhighlight lang="console">

$ declare -p foo bar declare -- foo="abc" declare -- bar="123" $ declare -p "$#" bash: declare: 0: not found $ </syntaxhighlight>

For a full string of input at an interactive shell...

<syntaxhighlight lang="console">

$ declare -p # </syntaxhighlight>

...the hashtag would be interpreted by Bash as an inline comment. With the comment and all text to the right of it removed, the command that Bash would execute would be Template:Code. This command would, according to Template:Code, "display the values and attributes of each NAME," i.e., each variable, and, "if no NAMEs are given, display the values and attributes and values of all variables," which can be over 100 lines of output.

On the other hand, Template:Mono cannot display variables' attributes. See also Template:Section link.

<syntaxhighlight lang="console">

$ readonly foo $ declare -p foo declare -r foo="abc" $ printf '<%s>' "${foo}" <abc> $ </syntaxhighlight>

Environment

Template:How-to Configurable execution environment(s):<ref> Template:Cite web</ref>

Shell and session startup Files (a.k.a., "dot files")

When Bash starts, it executes the commands in a variety of dot files.<ref name="drdobbs" /> Unlike Bash shell scripts, dot files typically have neither the execute permission enabled nor an interpreter directive like Template:Code.

  • Legacy-compatible Bash startup example

The example Template:Code below is compatible with the Bourne shell and gives semantics similar to csh for the Template:Code and Template:Code. The [ -r filename ] && cmd is a short-circuit evaluation that tests if filename exists and is readable, skipping the part after the Template:Code if it is not.

<syntaxhighlight lang="bash">

[ -r ~/.profile ] && ~/.profile # set up environment, once, Bourne-sh syntax only if [ -n "$PS1" ]; then # are we interactive?

  [ -r ~/.bashrc     ] && ~/.bashrc        # tty/prompt/function setup for interactive shells
  [ -r ~/.bash_login ] && ~/.bash_login    # any at-login tasks for login shell only

fi # End of "if" block </syntaxhighlight>

  • Operating system issues in Bash startup

Some versions of Unix and Linux contain Bash system startup scripts, generally under the Template:Code directory. Bash executes these files as part of its standard initialization, but other startup files can read them in a different order than the documented Bash startup sequence. The default content of the root user's files may also have issues, as well as the skeleton files the system provides to new user accounts upon setup. The startup scripts that launch the X window system may also do surprising things with the user's Bash startup scripts in an attempt to set up user-environment variables before launching the window manager. These issues can often be addressed using a Template:Code or Template:Code file to read the Template:Code — which provides the environment variables that Bash shell windows spawned from the window manager need, such as xterm or Gnome Terminal.

Standard streams

Standard streams - STDIN, STDOUT and STDERR

Commands

System commands

Aliases

Template:Blockquote

Keywords and reversed words

  • Template:Code
    • Bash function declarations which include this particular keyword are not compatible with Bourne/Korn/POSIX scripts, however, Bash does accepts the function declaration syntax used by Bourne, Korn and POSIX-compliant shells.

Functions

Template:Blockquote

Builtin commands

  • Various Built-In Commands:
    • POSIX Special builtins:<ref>

Template:Cite web</ref>

Template:Cite web</ref>

      • Xtrace: [ Template:Code | Template:Code ]. The shell's primary means of debugging. Both xtrace and verbose can be turned off at the same time with the command Template:Code.
      • Verbose: [ Template:Code | Template:Code ]. Prints a command to the terminal as Bash reads it. Bash reads constructs all at once, such as compound commands which include if-fi and case-esac blocks. If a Template:Code is included within a compound command, then "verbose" will be enabled the next time Bash reads code as input, i.e., after the end of the currently executing construct.<ref>

Template:Cite mailing list</ref>

      • Both xtrace and verbose can be turned off at the same time with the command Template:Code.
    • Template:Mono<ref name="bash-hackers.org_changes">

Template:Cite web</ref>

      • expand-aliases: On by default in interactive shells. Some developers discourage its use in scripts.

PATH and system commands

When the shell looks for external commands, it relies on the Bourne shell variable Template:Code. Template:Code contains a list of directories separated by colons, Template:Code. Beginning with the leftmost directory and selecting directories in a left to right pattern, each directory is searched until a match is found. In Linux, so that a user can locate additional commands, it's common practice for distribution administrators and package developers to alter the value of an end user's Template:Code by including source files in Template:Code and other locations.

When looking for the command, Template:Code, for instance, after considering internal commands and finding nothing, Bash will search the directories in Template:Code and will select the absolute path of the first executable found that has a basename which matches the search string.<ref name="tldp_3.2.1"/>

If there is more than one command Template:Code available in the directories listed in Template:Code, during the process of parsing and executing a commandline, by default only the first command found will be selected. Template:Code lookups are slow. The shell speeds up the commandline execution process by remembering command locations in a hash table. To perform a full Template:Code search without any interference from the hash table, remove the current table with Template:Code and search for all kinds of commands with Template:Code.

<syntaxhighlight lang="console">

$ # Force a full path search $ PATH=${PATH}:${HOME} $ printf 'echo script_file: "$@"\n' > ./echo $ chmod 0700 ./echo $ hash -r; type -a echo echo is a shell builtin echo is /usr/bin/echo echo is /home/liveuser/echo $ </syntaxhighlight>

In order to execute a commandline with a command found later in the Template:Code string, you can specify an absolute path or you can anchor path resolution relative to the current working directory.

<syntaxhighlight lang="console">

$ /home/liveuser/echo foo script_file: foo $ ./echo bar script_file: bar $ </syntaxhighlight>

For security reasons it is advisable to make sure the directories in PATH are not world-writeable, or are writeable only by root and trusted users.

Command lookup

Control structures

Template:See also-text

Subshells

Subshells: Template:Code;

Pipelines

However, by using a pipeline, they can engage in multiple cycles of computation at the same time, substantially increasing their speed. In a pipelined control unit, different instructions simultaneously go through the process but at different points. While one instruction is being fetched, a second is being decoded, and so forth. Unix-style pipelines: |.

Logical operators

Bash supplies "conditional execution" command separators that make execution of a command contingent on the exit code set by a precedent command. For example:

<syntaxhighlight lang="console"> $ cd "$SOMEWHERE" && ./do_something || echo "An error occurred" >&2 </syntaxhighlight>

Where Template:Code is only executed if the Template:Mono (change directory) command was "successful" (returned an exit status of zero) and the Template:Mono command would only be executed if either the Template:Mono or the Template:Code command return an "error" (non-zero exit status).

Iteration

Template:Main

Template:Blockquote

Compound commands

Template:Blockquote Bash also supports Template:Code and Template:Code forms of conditional command evaluation.Template:Efn

Testing

Built in commands for testing file attributes, comparing string and integer values, etc.:

For all commands the exit status is stored in the special variable Template:Code.

Regular Expressions

Bash 3.0 supports in-process regular expression matching using a syntax reminiscent of Perl.<ref> Template:Cite web</ref> Regexp matching is limited to strings on the right side of the =~ operator in the Template:Code extended test construct.<ref> Template:Cite web</ref>

<syntaxhighlight lang="bash" inline>[[ $line =~ space:*(a)?b ]]</syntaxhighlight> means values for line like 'aab', ' aaaaaab', 'xaby', and ' ab' will all match, as will a line containing a 'b' anywhere in its value.

Coprocesses

Template:Blockquote

Data manipulation

Word Splitting

Split into words (i.e., word splitting)

Quoting

Template:Blockquote

Bash has certain quoting rules: uses of

See also Template:Section link, Template:Code

See also backticks Template:Code: Template:Section link.

Unicode

Support for Unicode in Template:Code and ANSI-C quoting.

Brace Expansion

<syntaxhighlight lang="console">

$ echo kernel{,-headers} kernel kernel-headers </syntaxhighlight>

Brace expansion, also called alternation, is a feature copied from the C shell. It generates a set of alternative combinations.<ref> Template:Cite web</ref> Generated results need not exist as files. The results of each expanded string are not sorted and left to right order is preserved:

<syntaxhighlight lang="console">

$ echo a{p,c,d,b}e ape ace ade abe $ echo {a,b,c}{d,e,f} ad ae af bd be bf cd ce cf </syntaxhighlight>

Users should not use brace expansions in portable shell scripts, because the Bourne shell does not produce the same output.

<syntaxhighlight lang="console">

$ # bash shell $/bin/bash -c 'echo a{p,c,d,b}e' ape ace ade abe $ # A traditional shell does not produce the same output $ /bin/sh -c 'echo a{p,c,d,b}e' a{p,c,d,b}e </syntaxhighlight>

When brace expansion is combined with wildcards, the braces are expanded first, and then the resulting wildcards are substituted normally. Hence, a listing of JPEG and PNG images in the current directory could be obtained using:

<syntaxhighlight lang="bash">

ls *.{jpg,jpeg,png} # expands to *.jpg *.jpeg *.png – after which,

                      # the wildcards are processed

echo *.{png,jp{e,}g} # echo just shows the expansions –

                      # and braces in braces are possible.

</syntaxhighlight>

In addition to alternation, brace expansion can be used for sequential ranges between two integers or characters separated by double dots. Newer versions of Bash allow a third integer to specify the increment.

<syntaxhighlight lang="console">

$ echo {1..10} 1 2 3 4 5 6 7 8 9 10 $ echo {01..10} 01 02 03 04 05 06 07 08 09 10 $ echo file{1..4}.txt file1.txt file2.txt file3.txt file4.txt $ echo {a..e} a b c d e $ echo {1..10..3} 1 4 7 10 $ echo {a..j..3} a d g j </syntaxhighlight>

When brace expansion is combined with variable expansion (a.k.a., parameter expansion and parameter substitution) the variable expansion is performed after the brace expansion, which in some cases may necessitate the use of the Template:Mono built-in, thus:

<syntaxhighlight lang="console">

$ start=1; end=10 $ echo {$start..$end} # fails to expand due to the evaluation order {1..10} $ eval echo {$start..$end} # variable expansion occurs then resulting string is evaluated 1 2 3 4 5 6 7 8 9 10 </syntaxhighlight>

Tilde Expansion

Template:Empty section

Parameter and variable expansion

Pathname expansion

Pathname expansion, i.e., shell-style globbing and pattern matching using Template:Code, Template:Code, Template:Code.Template:Efn

Locales

Locale-specific translation via Template:Code quoting syntax.<ref> Template:Cite web</ref>

Process redirections and parsing

Command substitution

Command substitution: Template:Code,

Process substitution

Process substitution, Template:Code or Template:Code, when a system supports it:

Bash supports process substitution using the Template:Code and Template:Code syntax, which substitutes the output of (or input to) a command where a filename is normally used. (This is implemented through /proc/fd/ unnamed pipes on systems that support that, or via temporary named pipes where necessary).

Arithmetic expansion

Arithmetic expansion, Template:Code or Template:Code, including

Bash can perform integer calculations ("arithmetic evaluation") without spawning external processes. It uses the Template:Code command and the Template:Code variable syntax for this purpose.

Redirection

Redirections of Standard Input, Standard Output and Standard Error data streams are performed, including

Its syntax simplifies I/O redirection. For example, it can redirect standard output (stdout) and standard error (stderr) at the same time using the Template:Code operator. This is simpler to type than the Bourne shell equivalent 'Template:Code'. Bash supports here documents. Since version 2.05b Bash can redirect standard input (stdin) from a "here string" using the Template:Code operator.

Command parsing

  • (A) Comments are ignored, from an unquoted Template:Code (hash) to the end of the same line;<ref>

Template:Cite web</ref><ref> Template:Cite web</ref>

Interactive-only features

Command History

Unlimited size command history.<ref> Template:Cite web</ref> This feature is available in interactive mode only.

Directory stack

A directory stack ([[pushd and popd|Template:Code and Template:Code]] built-ins) feature is available in interactive mode only.

Programmable completion

Also known as "tab completion" or "command-line completion", when a user presses the Template:Keypress, within an interactive command-shell Bash automatically uses any available completion scripts to suggest partly typed program names, filenames and variable names.<ref> Template:Cite web</ref> <ref name =bashfaq061/> The Bash command-line completion system is very flexible and customizable, and is often packaged with functions that complete arguments and filenames for specific programs and tasks.

Bash supports programmable completion via built-in Template:Code, Template:Code, and Template:Code commands.<ref> Template:Cite web</ref> The feature has been available since the beta version of 2.04 released in 2000.<ref> Template:Cite web</ref> These commands enable complex and intelligent completion specification for commands (i.e., installed programs), functions, variables, and filenames.<ref name="tldp_pc"> Template:Cite web</ref>

The Template:Code and Template:Code two commands specify how arguments of some available commands or options are going to be listed in the readline input.As of version 5.1 completion of the command or the option is usually activated by the Template:Keypress keystroke after typing its name.<ref name =tldp_pc/> This feature is available in interactive mode only.

Prompts

Configurable prompts. This feature is available in interactive mode only.

Documentation

User Manual

A user manual for Bash is provided by the GNU Project. It is sometimes considered to be a more user-friendly document than the man page. "You may also find information about Bash ...by looking at Template:Code, Template:Code, or similar directories on your system."<ref name =gnu.org_bash-hm> Template:Cite web </ref> On GNU/Linux systems, if the Template:Mono program is available then the GNU Manual version relevant for your installation should also be available at Template:Code.<ref name="gnu.org_info"> Template:Cite web </ref><ref> Template:Cite web</ref>

Man page

The most recent technical manual, or 'man page', is intended to be the authoritative explanatory technical document for the understanding of how bash operates. On GNU/Linux systems, the version relevant for your installation is usually available through the Template:Mono program at Template:Code.<ref name =gnu.org_info/><ref name =case.edu_bash(1)/><ref> Template:Cite web </ref>

help builtin

With recent versions of Bash, information on shell built-in commands can be found by executing Template:Code, Template:Code or Template:Code at a terminal prompt where bash is installed.

The Template:Mono command can be invoked via Template:Mono to ensure that you run the program found via your shell's search path, and not a shell alias or built-in function: Template:Code.<ref> Template:Cite web</ref>

POSIX Specification

For the purpose of allowing inter-operability among different shell programs running on different operating systems, the POSIX Specification influences how modern UNIX-like shells are written. Bash "is intended to be a conformant implementation of the IEEE POSIX "Shell and Utilities" portion of the IEEE POSIX specification (IEEE Standard 1003.1)."<ref> Template:Cite web </ref> The most recent publication of the standard (2024) is available online.<ref> Template:Cite web </ref>

As the standard upon which bash is based, the POSIX Standard, or IEEE Std 1003.1,<ref> Template:Cite web</ref> et seq, is especially informative.

Further resources

"The project maintainer also has a Bash page which includes Frequently Asked Questions",<ref name="gnu.org_bash-hm"/><ref name="bash-top"> Template:Cite web</ref><ref> Template:Cite web</ref> this FAQ is current as of bash version 5.1 and is no longer updated.

Informal avenues of support are available via IRC at libera.chat, in the #bash channel, and mailing lists are available at Bash - GNU Project - Free Software Foundation.

Security and vulnerabilities

Root scripts

Running any shell scripts as the root user has, for years, been widely criticized as poor security practice. One commonly given reason is that, when a script is executed as root, the negative effects of any bugs in a script would be magnified by root's elevated privileges.

One common example: a script contains the command, Template:Code, but the variable Template:Code is left undefined. In Linux, if the script was executed by a regular user, the shell would attempt to execute the command Template:Code as a regular user, and the command would fail. However, if the script was executed by the root user, then the command would likely succeed and the filesystem would be erased.

It is recommended to use Template:Mono on a per-command basis instead.

CGI scripts

CGI scripts are a significant source of vulnerability.<ref> Template:Cite web</ref><ref> Template:Cite web</ref><ref> Template:Cite web</ref> Template:Clarify

"The eval command is extremely powerful and extremely easy to abuse."<ref> Template:Cite web</ref>

Input validation

"Input validation is the process of ensuring data has undergone data cleansing to confirm it has data quality, that is, that it is both correct and useful."

Template:Blockquote

Shellshock

In September 2014, a security bug was discovered<ref> Template:Cite news</ref> in the program. It was dubbed "Shellshock." Public disclosure quickly led to a range of attacks across the Internet.<ref> Template:Cite web </ref><ref> Template:Cite news</ref><ref> Template:Cite web</ref>

Exploitation of the vulnerability could enable arbitrary code execution in CGI scripts executable by certain versions of Bash. The bug involved how Bash passed function definitions to subshells through environment variables.<ref> Template:Cite web</ref> The bug had been present in the source code since August 1989 (version 1.03)<ref> Template:Cite web</ref> and was patched in September 2014 (version 4.3).

Patches to fix the bugs were made available soon after the bugs were identified. Upgrading to a current version is strongly advised.

It was assigned the Common Vulnerability identifiers Template:CVE, among others. Under CVSS Metrics 2.x and 3.x, the bug is regarded as "high" and "critical", respectively.

Deprecated syntax

Template:Unreferenced section

Debugging

Table of Features

Template:Sort-under

Bash features which can be useful during debugging.<ref name =bashfaq061/><ref name =bash-hackers.org_changes/><ref> Template:Cite web</ref>
Feature POSIX 2024 Description Bash ver.
Grammar type Formal name Syntax
Special Built-In Utility set / xtrace Template:Code Template:Yes The shell's primary means of debugging.

It "writes to standard error a trace for each command after it expands the command and before it executes it."

Template:Dunno
Special Parameters Exit Status Template:Code Template:Yes "Expands to the shortest representation of the decimal exit status." Template:Dunno
Parameter Expansions Indicate Null or Unset Template:Code Template:Yes "Where the expansion of Template:Code, perhaps an error message or a line number, is written to standard error and the shell exits with a non-zero exit code." Template:Dunno
Special Parameters PID of Invoked Shell Template:Code Template:Yes "Expands to the shortest representation of the decimal process ID of the invoked shell." Template:Dunno
Special Built-In Utility set / verbose Template:Code Template:Yes "Writes its input to standard error as it is read." Template:Dunno
Special Built-In Utility set / pipefail Template:Code Template:Yes "Derive the exit status of a pipeline from the exit statuses of all of the commands in the pipeline, not just the last (rightmost) command." Template:Dunno
Special Built-In Utility set / nounset Template:Code Template:Yes When enabled, will cause the shell to exit with an error message when it encounters an unset variable expansion.

Its use has a number of counter-intuitive pitfalls.

Template:Dunno
Special Built-In Utility set / errexit Template:Code Template:Yes Errexit is a setting that, when enabled, will, under certain very specific conditions, cause the shell to exit without an error message whenever the shell receives a non-zero exit code.

Its use is somewhat controversial, to the extent that any somewhat obscure computer program can be controversial. Adherents claim that Errexit provides an assurance of verifiability in situations where shell scripts "must not fail." However, opponents claim that its use is unreliable, deceptively simple, highly counter-intuitive, rife with gotchas and pitfalls, and in essence "security theater." Numerous developers of Bash have strongly discouraged the use of this particular setting.

Template:Dunno
Special Built-In Utility trap / EXIT Template:Code Template:Yes "If a signal specifier is Template:Code or Template:Code, Template:Code is executed when the shell exits." If Template:Code contains expansions, then Template:Code should be in single quotes. Template:Dunno
Utility printf Template:Code Template:Yes A means of reliably printing the contents of a variable. Template:Dunno
Bash Variables BASHPID Template:Code Template:No "Expands to the process ID of the current bash process."<ref>

Template:Cite web</ref>

Template:Dunno
Bash Variables BASH_ARGC Template:Code Template:No "An array variable whose values are the number of parameters in each frame of the current bash execution call stack."<ref>

Template:Cite web</ref>

Template:Dunno
Bash Variables BASH_ARGV Template:Code Template:No "An array variable containing all of the parameters in the current bash execution call stack."<ref>

Template:Cite web</ref>

Template:Dunno
Bash Variables BASH_LINENO Template:Code Template:No "An array variable whose members are the line numbers in source files where each corresponding member of Template:Code was invoked."<ref>

Template:Cite web</ref>

Template:Dunno
Bash Variables BASH_REMATCH Template:Code Template:No "An array variable whose members are assigned by the =~ binary operator to the [[ conditional command."<ref>

Template:Cite web</ref>

Template:Dunno
Bash Variables BASH_SOURCE Template:Code Template:No "An array variable whose members are the source filenames where the corresponding shell function names in the Template:Code array variable are defined."<ref>

Template:Cite web</ref>

Template:Dunno
Bash Variables BASH_XTRACEFD Template:Code Template:No "If set to an integer corresponding to a valid file descriptor, Bash will write the trace output generated when Template:Code is enabled to that file descriptor."<ref>

Template:Cite web</ref>

Template:Dunno
Bash Variables EPOCHREALTIME Template:Code Template:No "Each time this parameter is referenced, it expands to the number of seconds since the Unix Epoch (see Template:Code) as a floating point value with micro-second granularity."<ref>

Template:Cite web</ref>

Template:Dunno
Bash Variables FUNCNAME Template:Code Template:No "An array variable containing the names of all shell functions currently in the execution call stack."<ref>

Template:Cite web</ref>

Template:Dunno
Bash Variables LINENO Template:Code Template:No "Each time this parameter is referenced, the shell substitutes a decimal number representing the current sequential line number (starting with 1) within a script or function."<ref>

Template:Cite web</ref>

Template:Dunno
Bash Variables PIPESTATUS Template:Code Template:No "An array variable containing a list of exit status values from the processes in the most-recently-executed foreground pipeline (which may contain only a single command)."<ref>

Template:Cite web</ref>

Template:Dunno
Bash Variables PPID Template:Code Template:No "The process ID of the shell's parent."<ref>

Template:Cite web</ref>

Template:Dunno
Bash Variables PS4 Template:Code Template:No "The value of this parameter is expanded as with PS1 and the value is printed before each command bash displays during an execution trace."<ref>

Template:Cite web</ref>

Template:Dunno
Shell Builtin set / restricted Template:Code Template:No Restricted mode is intended to improve the security of an individual shell instance from a malicious human with physical access to a machine.

As threat models have changed, it has become less commonly used now than it once was.

Template:Dunno
Shell Builtin shopt / extdebug Template:Code Template:No "Behavior intended for use by debuggers." Template:Dunno
Shell Builtin trap / DEBUG Template:Code Template:No "If a sigspec is DEBUG, the command arg is executed before" certain kinds of commands. Template:Dunno
Shell Builtin trap / ERR Template:Code Template:No "If a sigspec is ERR, the command arg is executed whenever..." certain kinds of commands "return a non-zero exit status", subject to similar restrictions as with ErrExit. Template:Dunno
Shell Builtin trap / RETURN Template:Code Template:No "If a sigspec is RETURN, the command arg is executed each time a shell function or a script executed with the Template:Code or Template:Mono builtins finishes executing." Template:Dunno
  • Shell features specified by POSIX:
    • Parameter Expansions:<ref>

Template:Multiref2</ref>

    • Special Parameters:<ref>

Template:Cite web</ref><ref> Template:Cite web</ref>

    • Special Built-In Utility Template:Mono:<ref name="infoandposixset">

Template:Multiref2</ref><ref name="case.edu_bash1"> Template:Cite web</ref>

    • Special Built-In Utility Template:Mono:<ref name =case.edu_bash1/><ref name="gnu.org_trap">

Template:Cite web</ref>

      • POSIX does specify certain uses of the Template:Mono builtin: ...
    • Utility Template:Mono: a means of reliably printing the contents of a variable:
  • Bash features not specified by POSIX:
    • Bash Variables:<ref>

Template:Cite web</ref><ref> Template:Cite web</ref>

Template:Cite web</ref><ref name =case.edu_bash1/>

    • Shell Builtin Template:Mono:<ref name =gnu.org_trap/><ref name =case.edu_bash1/>
      • While POSIX does specify certain uses of the Template:Mono builtin, the following signal specs are Bash extensions: ...
  • Third party debugging utilities:
    • ShellCheck: Shell script analysis tool;<ref>

Template:Multiref2</ref><ref name =shellcheck-1/>

    • devscripts-checkbashisms: Check whether a /bin/sh script contains any common bash-specific constructs;<ref>

Template:Cite web</ref><ref name =checkbashisms-1/>

    • kcov: Code coverage tool without special compilation options;<ref>

Template:Cite web</ref>

    • Bashdb: The Bash symbolic debugger.<ref name =ref_bashdb/><ref>

Template:Cite mailing list</ref>

Examples

With the Template:Code parameter expansion, an unset or null variable can halt a script.

<syntaxhighlight lang="console">

$ cat ex.sh

  1. !/bin/bash

bar="foo is not defined" echo "${foo:?$bar}" echo this message doesn't print

$ ./ex.sh ./ex.sh: line 3: foo: foo is not defined $ </syntaxhighlight>

Reliably printing the contents of an array that contains spaces and newlines first in a portable syntax, and then the same thing in Bash. Note that POSIX doesn't have named array, only the list of arguments, Template:Code, which can be re-set by the Template:Mono builtin.

<syntaxhighlight lang="console">

$ # In POSIX shell: $ set -- "a" " b" " > c " $ printf ',%s,\n' "$@" ,a, , b, ,

c,

</syntaxhighlight>

Note that in Bash, the number of spaces before the newline is made clear.

<syntaxhighlight lang="console">

$ # In Bash: $ array=( "a" " b" " > c " ) $ declare -p array declare -a array=([0]="a" [1]=" b" [2]=$' \n c ') </syntaxhighlight>

Printing an error message when there's a problem.

<syntaxhighlight lang="console">

$ cat error.sh

  1. !/bin/env bash

if ! lsblk | grep sdb then

 echo Error, line "${LINENO}"

fi $ ./error.sh Error, line 130 </syntaxhighlight>

Using xtrace. If errexit had been enabled, then Template:Code would not have been executed.

<syntaxhighlight lang="console">

$ cat test.sh

  1. !/bin/env bash

set -x foo=bar; echo "${foo}" false echo quux $ ./test.sh + foo=bar + echo bar bar + false + echo quux quux </syntaxhighlight>

Note: Template:Code differs from Template:Code in certain circumstances, such as subshells that do not require bash to be reinitialized.

<syntaxhighlight lang="console">

$ echo $(echo $BASHPID $$) $$ $BASHPID

             25680    16920 16920 16920
  1. | | | |
  2. | | | \-- $BASHPID outside of the subshell
  3. | | \-- $$ outside of the subshell
  4. | \-- $$ inside of the subshell
  5. \-- $BASHPID inside of the subshell

</syntaxhighlight>

Bug reporting

Template:Redirect

An external command called bashbug reports Bash shell bugs. When the command is invoked, it brings up the user's default editor with a form to fill in. The form is mailed to the Bash maintainers (or optionally to other email addresses).<ref> Template:Cite web</ref><ref> Template:Cite web</ref>

History

Shell script functionality originated with files called "runcoms" in reference to the 1963 macro processor of the same name. The suffix "rc" is short for "runcom."<ref> Template:Cite web</ref> The term "shell" was coined by Louis Pouzin in 1964 or 1965, and appeared in his 1965 paper, "The SHELL, A Global Tool for Calling and Chaining Procedures in the System," which describes many features later found in many UNIX shells.<ref> Template:Cite web </ref><ref> Template:Cite web </ref> The ASCII standard for character encoding was defined in 1969 in a document called Request for Comments (RFC) 20.<ref> Template:Cite web</ref>

Timeline

Significant events in Bash history are listed below:

See also

Template:Portal

Template:Citation </ref>

Unix shells

Template:Div col

Template:Cite web</ref>

Template:Div col end

Graphical interface to scripts

There are many programs that allow you to create a graphical interface for shell scripts.

  • curses - curses is a terminal control library for Unix-like systems, enabling the construction of text user interfaces (TUI) applications.
  • dialog - is a utility that allows you to create dialog boxes in the console, using the curses and ncurses libraries.
  • gtkdialog - is the most functional utility for creating graphical applications on bash scripts.<ref>

Template:Cite web</ref>

  • kdialog - is a KDE equivalent of zenity.<ref name="linux-mag_sh-click">

Template:Cite web</ref>

  • ncurses - a programming library for creating textual user interfaces (TUI's) that work across a wide variety of terminals.
  • whiptail - is an analogue of the dialog utility, it uses the newt library.<ref>

Template:Cite web</ref>

  • xdialog - is a replacement for dialog that is designed to give programs launched from the terminal an X Window System interface.
  • yad - is a fork of zenity, with more features.<ref>

Template:Cite web</ref>

  • zenity - is the most popular application for creating a graphical interface for scripts.<ref name =linux-mag_sh-click/><ref>

Template:Cite web</ref>

Further reading

Notes

Template:Notelist

References

Template:Reflist

Template:GNU Template:Unix shells Template:Programming languages Template:Authority control