ASCII, strings and numbers $ printf ': \n' $'\n' : $ printf ': \n' $'\t' : $ printf ': \n' " " : $ printf ': \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 (i.e. backward) in the command history • – Scroll down (i.e. forward) 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: ||, &&, &, ;, ;;, ;&, ;;&, |, |&, (, 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. 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 $ ( echo 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. A non-comprehensive list of operators that cause the initialization of a subshell is as follows: • Subshells syntax, (), for example, would initialize a subshell:$ ( program arga argb ) • Piping syntax, A | B, for example, would also initialize a subshell: $ program1 abc def | program2 -abc
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. 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 {{Code| ${quux[@]} | bash}} or {{Code| ${quux[*]} | bash}} or {{Code| ${quux[1]} | bash}}, 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 {{mono|${foo}, PID, PWD, EUID, $$}}, {{mono|${quux} }} and {{mono|${zork} }}.
Execution "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 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 $ : "${foo}"; : "${bar}" + : aa + : bb $ : "${quux}" \ > : "${zork}" + : 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. $ # [COMMAND POSITION] [OPTION] [ARGUMENTS] $ # ,--^ ,------------^ ,----^ $ declare -p USER BASH_VERSION declare -x USER="liveuser" declare -- BASH_VERSION="5.2.37(1)-release" $
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 . For regular users, a common default value for is the dollar character, . For the
superuser, a common default value is hashtag () $ sudo --login --user root [sudo] password for liveuser: • vim /home/liveuser/names.txt • exit $ grep -e bob ./names.txt grep: ./names.txt: Permission denied == Modes ==