Bash (Unix shell)


Bash is an interactive command interpreter and scripting language developed for Unix-like operating systems. Created in 1989 by Brian Fox for the GNU Project, it is designed as a completely free software alternative for the Bourne shell,, and other proprietary Unix shells, supported by the Free Software Foundation. Having gained widespread adoption, Bash is commonly used as the default login shell for numerous Linux distributions. 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

$ printf ': <%b>\n' $'\n'
: <
$ printf ': <%b>\n' $'\t'
: < >
$ printf ': <%s>\n' " "
: < >
$ printf ': <%b>\n' $'\0'
: <>

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, can be used in filenames. In addition, all strings are case-sensitive.
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.
  • – Activate tab completion
  • – Scroll up in the command history
  • – Scroll down in the command history
  • – 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.
Default keybindings for control codes include:
  • – Move the cursor one character to the right
  • – Move the cursor one character to the left
  • – Move the cursor one word to the right
  • – Move the cursor one word to the left
  • – Move the cursor to the beginning of the current commandline
  • – Cancels the current command and presents a new prompt
  • – Closes the current Bash instance, possibly also closing the terminal-emulator
  • – Move the cursor to the end of the current commandline
  • – Wake the terminal; buffered keypresses are then processed
  • – Put the terminal to sleep
  • – Remove one word to the left of the cursor
  • – Stop a foregrounded process
Vi keybindings are also available and can be enabled by running.

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:
"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: ||, &&, &, ;, ;;, ;&, ;;&, |, |&, .
Redirection operators redirect the input or output streams. They include <, >, &>, <<, and <<<.

Words

A word is a sequence of 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.
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:

'!' case in esac for do done if then elif else fi...

Names may only contain the characters ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789_.
In the following example of a full command string, metacharacters have a comma placed above them,, reserved words have a caret placed beneath them,, and other tokens have a backtick placed also beneath them,.

$ #, , , ,, , ,, ,
$ if echo foo; then bar=abc; fi
$ # ^^ ```` ``` ^^^^ ``````` ^^

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.

$ echo foo
foo
$
foo

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.

Expansion

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. 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 held by the parameter from unwanted side effects.
Wrapping the variable name in curly brackets 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 $, 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 $0, so using $ 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. For example, expands to a list of the first through last positional parameters, "individually requoted, separated by spaces".
Environment variables are signified by all capital letters. Environment variables include UNIX variables like, and Bourne shell variables such as. Scripting variables are signified by all lower case letters or CamelCase. This is only convention; any variable can be passed to the command to be made into an environment variable.

Arrays

Arrays are data structures which hold multiple values. 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=. It is expanded using or or, depending on the use case.
Each kind of parameter is distinguished by a specific naming convention.
Since Bash 4.0, Bash also supports associative arrays.
In this article, examples of variables from this section include, and.

Execution

"Execution" of a given program occurs when a user 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 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,, 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.

$ foo=aa bar=bb quux=cc zork=dd; set -o xtrace
$ : "$"; : "$"
+ : aa
+ : bb
$ : "$" \
> : "$"
+ : cc : dd

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 variable and the current working directory.

$ #
$ #,--^ ,------------^ ,----^
$ declare -p USER BASH_VERSION
declare -x USER="liveuser"
declare -- BASH_VERSION="5.2.37-release"