bash Version 1.14.
Copyright (C) 1991, 1993 Free Software Foundation, Inc.
Bash is an acronym for Bourne Again SHell. The Bourne shell is the traditional Unix shell originally written by Stephen Bourne. All of the Bourne shell builtin commands are available in Bash, and the rules for evaluation and quoting are taken from the Posix 1003.2 specification for the `standard' Unix shell.
This section briefly summarizes things which Bash inherits from the Bourne shell: shell control structures, builtins, variables, and other features. It also lists the significant differences between Bash and the Bourne Shell.
Note that wherever you see a `;' in the description of a command's syntax, it may be replaced indiscriminately with one or more newlines.
Bash supports the following looping constructs.
until
until command is:
until test-commands; do consequent-commands; doneExecute consequent-commands as long as the final command in test-commands has an exit status which is not zero.
while
while command is:
while test-commands; do consequent-commands; doneExecute consequent-commands as long as the final command in test-commands has an exit status of zero.
for
for name [in words ...]; do commands; doneExecute commands for each member in words, with name bound to the current member. If "
in words" is not
present, "in "$@"" is assumed.
if
if command is:
if test-commands; then consequent-commands; [elif more-test-commands; then more-consequents;] [else alternate-consequents;] fiExecute consequent-commands only if the final command in test-commands has an exit status of zero. Otherwise, each
elif list is executed in turn,
and if its exit status is zero,
the corresponding more-consequents is executed and the
command completes.
If "else alternate-consequents" is present, and
the final command in the final if or elif clause
has a non-zero exit status, then execute alternate-consequents.
case
case command is:
case word in [pattern [| pattern]...) commands ;;]... esac
Selectively execute commands based upon word matching
pattern. The `|' is used to separate multiple patterns.
Here is an example using case in a script that could be used to
describe an interesting feature of an animal:
echo -n "Enter the name of an animal: " read ANIMAL echo -n "The $ANIMAL has " case $ANIMAL in horse | dog | cat) echo -n "four";; man | kangaroo ) echo -n "two";; *) echo -n "an unknown number of";; esac echo "legs."
Shell functions are a way to group commands for later execution using a single name for the group. They are executed just like a "regular" command. Shell functions are executed in the current shell context; no new process is created to interpret them.
Functions are declared using this syntax:
[ function ] name () { command-list; }
This defines a function named name. The body of the function is the command-list between { and }. This list is executed whenever name is specified as the name of a command. The exit status of a function is the exit status of the last command executed in the body.
When a function is executed, the arguments to the
function become the positional parameters
during its execution. The special parameter
# that gives the number of positional parameters
is updated to reflect the change. Positional parameter 0
is unchanged.
If the builtin command return
is executed in a function, the function completes and
execution resumes with the next command after the function
call. When a function completes, the values of the
positional parameters and the special parameter #
are restored to the values they had prior to function
execution.
The following shell builtin commands are inherited from the Bourne shell. These commands are implemented as specified by the Posix 1003.2 standard.
:
.
break
for, while, or until loop.
cd
continue
for, while,
or until loop.
echo
eval
exec
exit
export
getopts
hash
kill
pwd
read
readonly
return
shift
test
[
times
trap
umask
unset
wait
Bash uses certain shell variables in the same way as the Bourne shell. In some cases, Bash assigns a default value to the variable.
IFS
PATH
HOME
CDPATH
cd command.
MAILPATH
$_ stands for the name of the current mailfile.
PS1
PS2
OPTIND
getopts builtin.
OPTARG
getopts builtin.
Bash implements essentially the same grammar, parameter and variable expansion, redirection, and quoting as the Bourne Shell. Bash uses the Posix 1003.2 standard as the specification of how these features are to be implemented. There are some differences between the traditional Bourne shell and the Posix standard; this section quickly details the differences of significance. A number of these differences are explained in greater depth in subsequent sections.
Bash implements the ! keyword to negate the return value of
a pipeline. Very useful when an if statement needs to act
only if a test fails.
Bash includes brace expansion (see section Brace Expansion).
Bash includes the Posix and ksh-style pattern removal %% and
## constructs to remove leading or trailing substrings from
variables.
The Posix and ksh-style $() form of command substitution is
implemented, and preferred to the Bourne shell's " (which
is also implemented for backwards compatibility).
Variables present in the shell's initial environment are automatically
exported to child processes. The Bourne shell does not normally do
this unless the variables are explicitly marked using the export
command.
The expansion ${#xx}, which returns the length of $xx,
is supported.
The IFS variable is used to split only the results of expansion,
not all words. This closes a longstanding shell security hole.
It is possible to have a variable and a function with the same name;
sh does not separate the two name spaces.
Bash functions are permitted to have local variables, and thus useful recursive functions may be written.
The noclobber option is available to avoid overwriting existing
files with output redirection.
Bash allows you to write a function to override a builtin, and provides
access to that builtin's functionality within the function via the
builtin and command builtins.
The command builtin allows selective disabling of functions
when command lookup is performed.
Individual builtins may be enabled or disabled using the enable
builtin.
Functions may be exported to children via the environment.
The Bash read builtin will read a line ending in \ with
the -r option, and will use the $REPLY variable as a
default if no arguments are supplied.
The return builtin may be used to abort execution of scripts
executed with the . or source builtins.
The umask builtin allows symbolic mode arguments similar to
those accepted by chmod.
The test builtin is slightly different, as it implements the
Posix 1003.2 algorithm, which specifies the behavior based on the
number of arguments.
The C-Shell (csh) was created by Bill Joy at UC Berkeley. It
is generally considered to have better features for interactive use than
the original Bourne shell. Some of the csh features present in
Bash include job control, history expansion, `protected' redirection, and
several variables for controlling the interactive behaviour of the shell
(e.g. IGNOREEOF).
See section Using History Interactively for details on history expansion.
Bash has tilde (~) expansion, similar, but not identical, to that of
csh. The following table shows what unquoted words beginning
with a tilde expand to.
~
$HOME.
~/foo
~fred/foo
foo of the home directory of the user
fred.
~+/foo
~-
Bash will also tilde expand words following redirection operators and words following `=' in assignment statements.
Brace expansion is a mechanism by which arbitrary strings may be generated. This mechanism is similar to pathname expansion (see the Bash manual page for details), but the file names generated need not exist. Patterns to be brace expanded take the form of an optional preamble, followed by a series of comma-separated strings between a pair of braces, followed by an optional postamble. The preamble is prepended to each string contained within the braces, and the postamble is then appended to each resulting string, expanding left to right.
Brace expansions may be nested. The results of each expanded string are not sorted; left to right order is preserved. For example,
a{d,c,b}e
expands into ade ace abe.
Brace expansion is performed before any other expansions, and any characters special to other expansions are preserved in the result. It is strictly textual. Bash does not apply any syntactic interpretation to the context of the expansion or the text between the braces.
A correctly-formed brace expansion must contain unquoted opening and closing braces, and at least one unquoted comma. Any incorrectly formed brace expansion is left unchanged.
This construct is typically used as shorthand when the common prefix of the strings to be generated is longer than in the above example:
mkdir /usr/local/src/bash/{old,new,dist,bugs}
or
chown root /usr/{ucb/{ex,edit},lib/{ex?.?*,how_ex}}
Bash has several builtin commands whose definition is very similar
to csh.
pushd
pushd [dir | +n | -n]Save the current directory on a list and then
cd to
dir. With no
arguments, exchanges the top two directories.
+n
dirs) to the top of the list by rotating
the stack.
-n
dirs) to the top of the list by rotating
the stack.
dir
dirs command.
popd
popd [+n | -n]Pops the directory stack, and
cds to the new top directory. When
no arguments are given, removes the top directory from the stack and
cds to the new top directory. The
elements are numbered from 0 starting at the first directory listed with
dirs; i.e. popd is equivalent to popd +0.
+n
dirs), starting with zero.
-n
dirs), starting with zero.
dirs
dirs [+n | -n] [-l]Display the list of currently remembered directories. Directories find their way onto the list with the
pushd command; you can get
back up through the list with the popd command.
+n
dirs when invoked without options), starting
with zero.
-n
dirs when invoked without options), starting
with zero.
-l
history
history [n] [ [-w -r -a -n] [filename]]Display the history list with line numbers. Lines prefixed with with a
* have been modified. An argument of n says
to list only the last n lines. Option -w means
write out the current history to the history file; -r
means to read the current history file and make its contents the
history list. An argument of -a means to append the new
history lines (history lines entered since the beginning of the
current Bash session) to the history file. Finally, the
-n argument means to read the history lines not already
read from the history file into the current history list. These
are lines appended to the history file since the beginning of the
current Bash session. If filename is given, then it is used
as the history file, else if $HISTFILE has a value,
that is used, otherwise `~/.bash_history' is used.
logout
source
. (see section Bourne Shell Builtins)
IGNOREEOF
EOFs Bash will read before exiting. By default, Bash will exit
upon reading a single EOF.
cdable_vars
cd command
which are not directories as names of variables whose values are the
directories to change to.
This section describes features primarily inspired by the
Korn Shell (ksh). In some cases, the Posix 1003.2
standard has adopted these commands and variables from the
Korn Shell; Bash implements those features using the Posix
standard as a guide.
Bash includes the Korn Shell select construct. This construct
allows the easy generation of menus. It has almost the same syntax as
the for command.
The syntax of the select command is:
select name [in words ...]; do commands; done
The list of words following in is expanded, generating a list
of items. The set of expanded words is printed on the standard
error, each preceded by a number. If the "in words"
is omitted, the positional parameters are printed. The
PS3 prompt is then displayed and a line is read from the standard
input. If the line consists of the number corresponding to one of
the displayed words, then the value of name
is set to that word. If the line is empty, the words and prompt
are displayed again. If EOF is read, the select
command completes. Any other value read causes name
to be set to null. The line read is saved in the variable
REPLY.
The commands are executed after each selection until a
break or return command is executed, at which
point the select command completes.
This section describes Bash builtin commands taken from ksh.
fc
Fix Command. In the first form, a range of commands from first to last is selected from the history list. Both first and last may be specified as a string (to locate the most recent command beginning with that string) or as a number (an index into the history list, where a negative number is used as an offset from the current command number). If last is not specified it is set to first. If first is not specified it is set to the previous command for editing and -16 for listing. If thefc [-e ename] [-nlr] [first] [last]fc -s [pat=rep] [command]
-l flag is
given, the commands are listed on standard output. The -n flag
suppresses the command numbers when listing. The -r flag
reverses the order of the listing. Otherwise, the editor given by
ename is invoked on a file containing those commands. If
ename is not given, the value of the following variable expansion
is used: ${FCEDIT:-${EDITOR:-vi}}. This says to use the
value of the FCEDIT variable if set, or the value of the
EDITOR variable if that is set, or vi if neither is set.
When editing is complete, the edited commands are echoed and executed.
In the second form, command is re-executed after each instance
of pat in the selected command is replaced by rep.
A useful alias to use with the fc command is r='fc -s', so
that typing r cc runs the last command beginning with cc
and typing r re-executes the last command (see section Aliases).
let
let builtin allows arithmetic to be performed on shell variables.
For details, refer to section Arithmetic Builtins.
typeset
typeset command is supplied for compatibility with the Korn
shell; however, it has been made obsolete by the
declare command (see section Bash Builtin Commands).
REPLY
read builtin.
RANDOM
SECONDS
PS3
select command.
PS4
-x option is set (see section The Set Builtin).
PWD
cd builtin.
OLDPWD
cd builtin.
TMOUT
The shell maintains a list of aliases
that may be set and unset with the alias and
unalias builtin commands.
The first word of each command, if unquoted,
is checked to see if it has an
alias. If so, that word is replaced by the text of the alias.
The alias name and the replacement text may contain any valid
shell input, including shell metacharacters, with the exception
that the alias name may not contain =.
The first word of the replacement text is tested for
aliases, but a word that is identical to an alias being expanded
is not expanded a second time. This means that one may alias
ls to "ls -F",
for instance, and Bash does not try to recursively expand the
replacement text. If the last character of the alias value is a
space or tab character, then the next command word following the
alias is also checked for alias expansion.
Aliases are created and listed with the alias
command, and removed with the unalias command.
There is no mechanism for using arguments in the replacement text,
as in csh.
If arguments are needed, a shell function should be used.
Aliases are not expanded when the shell is not interactive.
The rules concerning the definition and use of aliases are
somewhat confusing. Bash
always reads at least one complete line
of input before executing any
of the commands on that line. Aliases are expanded when a
command is read, not when it is executed. Therefore, an
alias definition appearing on the same line as another
command does not take effect until the next line of input is read.
This means that the commands following the alias definition
on that line are not affected by the new alias.
This behavior is also an issue when functions are executed.
Aliases are expanded when the function definition is read,
not when the function is executed, because a function definition
is itself a compound command. As a consequence, aliases
defined in a function are not available until after that
function is executed. To be safe, always put
alias definitions on a separate line, and do not use alias
in compound commands.
Note that for almost every purpose, aliases are superseded by shell functions.
alias
alias [name[=value] ...]Without arguments, print the list of aliases on the standard output. If arguments are supplied, an alias is defined for each name whose value is given. If no value is given, the name and value of the alias is printed.
unalias
unalias [-a] [name ... ]Remove each name from the list of aliases. If
-a is
supplied, all aliases are removed.
This section describes the features unique to Bash.
In addition to the single-character shell command-line options (see section The Set Builtin), there are several multi-character options that you can use. These options must appear on the command line before the single-character options to be recognized.
-norc
sh.
-rcfile filename
-noprofile
-version
-login
csh. If you wanted to replace your
current login shell with a Bash login shell, you would say
`exec bash -login'.
-nobraceexpansion
-nolineediting
-posix
There are several single-character options you can give which are
not available with the set builtin.
-c string
-i
-s
An interactive shell is one whose input and output are both
connected to terminals (as determined by isatty()), or one
started with the -i option.
When and how Bash executes startup files.
For Login shells (subject to the -noprofile option):
On logging in:
If `/etc/profile' exists, then source it.
If `~/.bash_profile' exists, then source it,
else if `~/.bash_login' exists, then source it,
else if `~/.profile' exists, then source it.
On logging out:
If `~/.bash_logout' exists, source it.
For non-login interactive shells (subject to the -norc and -rcfile options):
On starting up:
If `~/.bashrc' exists, then source it.
For non-interactive shells:
On starting up:
If the environment variable ENV is non-null, expand the
variable and source the file named by the value. If Bash is
not started in Posix mode, it looks for BASH_ENV before
ENV.
So, typically, your ~/.bash_profile contains the line
if [ -f ~/.bashrc ]; then source ~/.bashrc; fi
after (or before) any login specific initializations.
If Bash is invoked as sh, it tries to mimic the behavior of
sh as closely as possible. For a login shell, it attempts to
source only `/etc/profile' and `~/.profile', in that order.
The -noprofile option may still be used to disable this behavior.
A shell invoked as sh does not attempt to source any other
startup files.
When Bash is started in POSIX mode, as with the
-posix command line option, it follows the Posix 1003.2
standard for startup files. In this mode, the ENV
variable is expanded and that file sourced; no other startup files
are read.
You may wish to determine within a startup script whether Bash is
running interactively or not. To do this, examine the variable
$PS1; it is unset in non-interactive shells, and set in
interactive shells. Thus:
if [ -z "$PS1" ]; then echo This shell is not interactive else echo This shell is interactive fi
You can ask an interactive Bash to not run your `~/.bashrc' file
with the -norc flag. You can change the name of the
`~/.bashrc' file to any other file name with -rcfile
filename. You can ask Bash to not run your
`~/.bash_profile' file with the -noprofile flag.
This section describes builtin commands which are unique to or have been extended in Bash.
builtin
builtin [shell-builtin [args]]Run a shell builtin. This is useful when you wish to rename a shell builtin to be a function, but need the functionality of the builtin within the function itself.
bind
bind [-m keymap] [-lvd] [-q name] bind [-m keymap] -f filename bind [-m keymap] keyseq:function-nameDisplay current Readline (see section Command Line Editing) key and function bindings, or bind a key sequence to a Readline function or macro. The binding syntax accepted is identical to that of `.inputrc' (see section Readline Init File), but each binding must be passed as a separate argument: `"\C-x\C-r":re-read-init-file'. Options, if supplied, have the following meanings:
-m keymap
emacs,
emacs-standard,
emacs-meta,
emacs-ctlx,
vi,
vi-move,
vi-command, and
vi-insert.
vi is equivalent to vi-command;
emacs is equivalent to emacs-standard.
-l
-v
-d
-f filename
-q
command
command [-pVv] command [args ...]Runs command with arg ignoring shell functions. If you have a shell function called
ls, and you wish to call
the command ls, you can say `command ls'. The
-p option means to use a default value for $PATH
that is guaranteed to find all of the standard utilities.
If either the -V or -v option is supplied, a
description of command is printed. The -v option
causes a single word indicating the command or file name used to
invoke command to be printed; the -V option produces
a more verbose description.
declare
declare [-frxi] [name[=value]]Declare variables and/or give them attributes. If no names are given, then display the values of variables instead.
-f means to use function names only. -r says to
make names readonly. -x says to mark names
for export. -i says that the variable is to be treated as
an integer; arithmetic evaluation (see section Shell Arithmetic) is
performed when the variable is assigned a value. Using +
instead of - turns off the attribute instead. When used in
a function, declare makes names local, as with the
local command.
enable
enable [-n] [-a] [name ...]Enable and disable builtin shell commands. This allows you to use a disk command which has the same name as a shell builtin. If
-n is used, the names become disabled. Otherwise
names are enabled. For example, to use the test binary
found via $PATH instead of the shell builtin version, type
`enable -n test'. The -a option means to list
each builtin with an indication of whether or not it is enabled.
help
help [pattern]Display helpful information about builtin commands. If pattern is specified,
help gives detailed help
on all commands matching pattern, otherwise a list of
the builtins is printed.
local
local name[=value]For each argument, create a local variable called name, and give it value.
local can only be used within a function; it makes the variable
name have a visible scope restricted to that function and its
children.
type
type [-all] [-type | -path] [name ...]For each name, indicate how it would be interpreted if used as a command name. If the
-type flag is used, type returns a single word
which is one of "alias", "function", "builtin", "file" or
"keyword", if name is an alias, shell function, shell builtin,
disk file, or shell reserved word, respectively.
If the -path flag is used, type either returns the name
of the disk file that would be executed, or nothing if -type
would not return "file".
If the -all flag is used, returns all of the places that contain
an executable named file. This includes aliases and functions,
if and only if the -path flag is not also used.
Type accepts -a, -t, and -p as equivalent to
-all, -type, and -path, respectively.
ulimit
ulimit [-acdmstfpnuvSH] [limit]
Ulimit provides control over the resources available to processes
started by the shell, on systems that allow such control. If an
option is given, it is interpreted as follows:
-S
-H option is not given).
-H
-a
-c
-d
-m
-s
-t
-f
-p
-n
-u
-v
This builtin is so overloaded that it deserves its own section.
set
set [-abefhkmnptuvxldCHP] [-o option] [argument ...]
-a
-b
-e
-f
-h
-k
-m
-n
-o option-name
allexport
-a.
braceexpand
emacs
errexit
-e.
histexpand
-H.
ignoreeof
interactive-comments
monitor
-m.
noclobber
-C.
noexec
-n.
noglob
-f.
nohash
-d.
notify
-b.
nounset
-u.
physical
-P.
posix
privileged
-p.
verbose
-v.
vi
vi-style line editing interface.
xtrace
-x.
-p
$ENV
file is not processed, and shell functions
are not inherited from the environment. This is enabled automatically
on startup if the effective user (group) id is not equal to the real
user (group) id. Turning this option off causes the effective user
and group ids to be set to the real user and group ids.
-t
-u
-v
-x
-l
for command.
-d
-C
-H
-P
cd which change the current directory. The physical directory
is used instead.
--
-.
-
-x
and -v options are turned off.
If there are no arguments, the positional parameters remain unchanged.
$-. The
remaining N arguments are positional parameters and are
assigned, in order, to $1, $2, .. $N. If
no arguments are given, all shell variables are printed.
These variables are set or used by bash, but other shells do not normally treat them specially.
HISTCONTROL
history_control
HISTFILE
HISTSIZE
histchars
HISTCMD
HISTCMD is unset, it loses its special properties,
even if it is subsequently reset.
hostname_completion_file
HOSTFILE
MAILCHECK
MAILPATH.
PROMPT_COMMAND
$PS1).
UID
EUID
HOSTTYPE
OSTYPE
FIGNORE
FIGNORE
is excluded from the list of matched file names. A sample
value is `.o:~'
INPUTRC
BASH_VERSION
IGNOREEOF
EOF character
as the sole input. If set, then the value of it is the number
of consecutive EOF characters that can be read as the
first characters on an input line
before the shell will exit. If the variable exists but does not
have a numeric value (or has no value) then the default is 10.
If the variable does not exist, then EOF signifies the end of
input to the shell. This is only in effect for interactive shells.
no_exit_on_failed_exec
exec command.
nolinks
cd which change the current directory.
For example, if `/usr/sys' is a link to `/usr/local/sys' then:
$ cd /usr/sys; echo $PWD /usr/sys $ cd ..; pwd /usrIf
nolinks exists, then:
$ cd /usr/sys; echo $PWD /usr/local/sys $ cd ..; pwd /usr/localSee also the description of the
-P option to the set
builtin, section The Set Builtin.
The shell allows arithmetic expressions to be evaluated, as one of
the shell expansions or by the let builtin.
Evaluation is done in long integers with no check for overflow, though division by 0 is trapped and flagged as an error. The following list of operators is grouped into levels of equal-precedence operators. The levels are listed in order of decreasing precedence.
- +
! ~
* / %
+ -
<< >>
<= >= < >
== !=
&
^
|
&&
||
= *= /= %= += -= <<= >>= &= ^= |=
Shell variables are allowed as operands; parameter expansion is performed before the expression is evaluated. The value of a parameter is coerced to a long integer within an expression. A shell variable need not have its integer attribute turned on to be used in an expression.
Constants with a leading 0 are interpreted as octal numbers.
A leading 0x or 0X denotes hexadecimal. Otherwise,
numbers take the form [base#]n, where base is a
decimal number between 2 and 36 representing the arithmetic
base, and n is a number in that base. If base is
omitted, then base 10 is used.
Operators are evaluated in order of precedence. Sub-expressions in parentheses are evaluated first and may override the precedence rules above.
Arithmetic expansion allows the evaluation of an arithmetic expression and the substitution of the result. There are two formats for arithmetic expansion:
$[ expression ] $(( expression ))
The expression is treated as if it were within double quotes, but a double quote inside the braces or parentheses is not treated specially. All tokens in the expression undergo parameter expansion, command substitution, and quote removal. Arithmetic substitutions may be nested.
The evaluation is performed according to the rules listed above. If the expression is invalid, Bash prints a message indicating failure and no substitution occurs.
let
let expression [expression]The
let builtin allows arithmetic to be performed on shell
variables. Each expression is evaluated according to the
rules given previously (see section Arithmetic Evaluation). If the
last expression evaluates to 0, let returns 1;
otherwise 0 is returned.
The value of the variable $PROMPT_COMMAND is examined just before
Bash prints each primary prompt. If it is set and non-null, then the
value is executed just as if you had typed it on the command line.
In addition, the following table describes the special characters which
can appear in the PS1 variable:
\t
\d
\n
\s
$0 (the portion
following the final slash).
\w
\W
$PWD.
\u
\h
\#
\!
\nnn
nnn.
\$
#, otherwise $.
\\
\[
\]
This chapter disusses what job control is, how it works, and how Bash allows you to access its facilities.
Job control refers to the ability to selectively stop (suspend) the execution of processes and continue (resume) their execution at a later point. A user typically employs this facility via an interactive interface supplied jointly by the system's terminal driver and Bash.
The shell associates a job with each pipeline. It keeps a
table of currently executing jobs, which may be listed with the
jobs command. When Bash starts a job
asynchronously (in the background), it prints a line that looks
like:
[1] 25647
indicating that this job is job number 1 and that the process ID of the last process in the pipeline associated with this job is 25647. All of the processes in a single pipeline are members of the same job. Bash uses the job abstraction as the basis for job control.
To facilitate the implementation of the user interface to job
control, the system maintains the notion of a current terminal
process group ID. Members of this process group (processes whose
process group ID is equal to the current terminal process group
ID) receive keyboard-generated signals such as SIGINT.
These processes are said to be in the foreground. Background
processes are those whose process group ID differs from the
terminal's; such processes are immune to keyboard-generated
signals. Only foreground processes are allowed to read from or
write to the terminal. Background processes which attempt to
read from (write to) the terminal are sent a SIGTTIN
(SIGTTOU) signal by the terminal driver, which, unless
caught, suspends the process.
If the operating system on which Bash is running supports
job control, Bash allows you to use it. Typing the
suspend character (typically `^Z', Control-Z) while a
process is running causes that process to be stopped and returns
you to Bash. Typing the delayed suspend character
(typically `^Y', Control-Y) causes the process to be stopped
when it attempts to read input from the terminal, and control to
be returned to Bash. You may then manipulate the state of
this job, using the bg command to continue it in the
background, the fg command to continue it in the
foreground, or the kill command to kill it. A `^Z'
takes effect immediately, and has the additional side effect of
causing pending output and typeahead to be discarded.
There are a number of ways to refer to a job in the shell. The
character `%' introduces a job name. Job number n
may be referred to as `%n'. A job may also be referred to
using a prefix of the name used to start it, or using a substring
that appears in its command line. For example, `%ce' refers
to a stopped ce job. Using `%?ce', on the
other hand, refers to any job containing the string `ce' in
its command line. If the prefix or substring matches more than one job,
Bash reports an error. The symbols `%%' and
`%+' refer to the shell's notion of the current job, which
is the last job stopped while it was in the foreground. The
previous job may be referenced using `%-'. In output
pertaining to jobs (e.g., the output of the jobs command),
the current job is always flagged with a `+', and the
previous job with a `-'.
Simply naming a job can be used to bring it into the foreground: `%1' is a synonym for `fg %1' bringing job 1 from the background into the foreground. Similarly, `%1 &' resumes job 1 in the background, equivalent to `bg %1'
The shell learns immediately whenever a job changes state.
Normally, Bash waits until it is about to print a prompt
before reporting changes in a job's status so as to not interrupt
any other output. If the
the -b option to the set builtin is set,
Bash reports such changes immediately (see section The Set Builtin).
This feature is also controlled by the variable notify.
If you attempt to exit bash while jobs are stopped, the
shell prints a message warning you. You may then use the
jobs command to inspect their status. If you do this, or
try to exit again immediately, you are not warned again, and the
stopped jobs are terminated.
bg
bg [jobspec]Place jobspec into the background, as if it had been started with `&'. If jobspec is not supplied, the current job is used.
fg
fg [jobspec]Bring jobspec into the foreground and make it the current job. If jobspec is not supplied, the current job is used.
jobs
jobs [-lpn] [jobspec] jobs -x command [jobspec]The first form lists the active jobs. The
-l option lists
process IDs in addition to the normal information; the -p
option lists only the process ID of the job's process group
leader. The -n option displays only jobs that have
changed status since last notfied. If jobspec is given,
output is restricted to information about that job.
If jobspec is not supplied, the status of all jobs is
listed.
If the -x option is supplied, jobs replaces any
jobspec found in command or arguments with the
corresponding process group ID, and executes command,
passing it arguments, returning its exit status.
suspend
suspend [-f]Suspend the execution of this shell until it receives a
SIGCONT signal. The -f option means to suspend
even if the shell is a login shell.
When job control is active, the kill and wait
builtins also accept jobspec arguments.
auto_resume
exact,
the string supplied must match the name of a stopped job exactly;
if set to substring,
the string supplied needs to match a substring of the name of a
stopped job. The substring value provides functionality
analogous to the %? job id (see section Job Control Basics).
If set to any other value, the supplied string must
be a prefix of a stopped job's name; this provides functionality
analogous to the % job id.
notify
This chapter describes how to use the GNU History Library interactively, from a user's standpoint. It should be considered a user's guide. For information on using the GNU History Library in your own programs, see the GNU Readline Library Manual.
The History library provides a history expansion feature that is similar
to the history expansion provided by csh. The following text
describes the syntax used to manipulate the history information.
History expansion takes place in two parts. The first is to determine which line from the previous history should be used during substitution. The second is to select portions of that line for inclusion into the current one. The line selected from the previous history is called the event, and the portions of that line that are acted upon are called words. The line is broken into words in the same fashion that Bash does, so that several English (or Unix) words surrounded by quotes are considered as one word.
An event designator is a reference to a command line entry in the history list.
!
!!
!-1.
!n
!-n
!string
!?string[?]
!#
^string1^string2^
!!:s/string1/string2/.
A : separates the event specification from the word designator. It can be omitted if the word designator begins with a ^, $, * or %. Words are numbered from the beginning of the line, with the first word being denoted by a 0 (zero).
0 (zero)
0th word. For many applications, this is the command word.
n
^
$
%
?string? search.
x-y
-y abbreviates 0-y.
*
0th. This is a synonym for 1-$.
It is not an error to use * if there is just one word in the event;
the empty string is returned in that case.
x*
x-$
x-
x-$ like x*, but omits the last word.
After the optional word designator, you can add a sequence of one or more of the following modifiers, each preceded by a :.
h
r
e
t
p
q
x
q,
but break into words at spaces, tabs, and newlines.
s/old/new/
&
g
s, as in gs/old/new/, or with
&.
This chapter describes the basic features of the GNU command line editing interface.
The following paragraphs describe the notation used to represent keystrokes.
The text C-k is read as `Control-K' and describes the character produced when the Control key is depressed and the k key is struck.
The text M-k is read as `Meta-K' and describes the character produced when the meta key (if you have one) is depressed, and the k key is struck. If you do not have a meta key, the identical keystroke can be generated by typing ESC first, and then typing k. Either process is known as metafying the k key.
The text M-C-k is read as `Meta-Control-k' and describes the character produced by metafying C-k.
In addition, several keys have their own names. Specifically, DEL, ESC, LFD, SPC, RET, and TAB all stand for themselves when seen in this text, or in an init file (see section Readline Init File, for more info).
Often during an interactive session you type in a long line of text, only to notice that the first word on the line is misspelled. The Readline library gives you a set of commands for manipulating the text as you type it in, allowing you to just fix your typo, and not forcing you to retype the majority of the line. Using these editing commands, you move the cursor to the place that needs correction, and delete or insert the text of the corrections. Then, when you are satisfied with the line, you simply press RETURN. You do not have to be at the end of the line to press RETURN; the entire line is accepted regardless of the location of the cursor within the line.
In order to enter characters into the line, simply type them. The typed character appears where the cursor was, and then the cursor moves one space to the right. If you mistype a character, you can use your erase character to back up and delete the mistyped character.
Sometimes you may miss typing a character that you wanted to type, and not notice your error until you have typed several other characters. In that case, you can type C-b to move the cursor to the left, and then correct your mistake. Afterwards, you can move the cursor to the right with C-f.
When you add text in the middle of a line, you will notice that characters to the right of the cursor are `pushed over' to make room for the text that you have inserted. Likewise, when you delete text behind the cursor, characters to the right of the cursor are `pulled back' to fill in the blank space created by the removal of the text. A list of the basic bare essentials for editing the text of an input line follows.
The above table describes the most basic possible keystrokes that you need in order to do editing of the input line. For your convenience, many other commands have been added in addition to C-b, C-f, C-d, and DEL. Here are some commands for moving more rapidly about the line.
Notice how C-f moves forward a character, while M-f moves forward a word. It is a loose convention that control keystrokes operate on characters while meta keystrokes operate on words.
Killing text means to delete the text from the line, but to save it away for later use, usually by yanking (re-inserting) it back into the line. If the description for a command says that it `kills' text, then you can be sure that you can get the text back in a different (or the same) place later.
When you use a kill command, the text is saved in a kill-ring. Any number of consecutive kills save all of the killed text together, so that when you yank it back, you get it all. The kill ring is not line specific; the text that you killed on a previously typed line is available to be yanked back later, when you are typing another line.
Here is the list of commands for killing text.
And, here is how to yank the text back into the line. Yanking means to copy the most-recently-killed text from the kill buffer.
You can pass numeric arguments to Readline commands. Sometimes the argument acts as a repeat count, other times it is the sign of the argument that is significant. If you pass a negative argument to a command which normally acts in a forward direction, that command will act in a backward direction. For example, to kill text back to the start of the line, you might type M-- C-k.
The general way to pass numeric arguments to a command is to type meta digits before the command. If the first `digit' you type is a minus sign (-), then the sign of the argument will be negative. Once you have typed one meta digit to get the argument started, you can type the remainder of the digits, and then the command. For example, to give the C-d command an argument of 10, you could type M-1 0 C-d.
Although the Readline library comes with a set of Emacs-like
keybindings installed by default,
it is possible that you would like to use a different set
of keybindings. You can customize programs that use Readline by putting
commands in an init file in your home directory. The name of this
file is taken from the value of the shell variable INPUTRC. If
that variable is unset, the default is `~/.inputrc'.
When a program which uses the Readline library starts up, the init file is read, and the key bindings are set.
In addition, the C-x C-r command re-reads this init file, thus
incorporating any changes that you might have made to it.
There are only a few basic constructs allowed in the Readline init file. Blank lines are ignored. Lines beginning with a # are comments. Lines beginning with a $ indicate conditional constructs (see section Conditional Init Constructs). Other lines denote variable settings and key bindings.
set command within the init file. Here is how you
would specify that you wish to use vi line editing commands:
set editing-mode viRight now, there are only a few variables which can be set; so few, in fact, that we just list them here:
editing-mode
editing-mode variable controls which editing mode you are
using. By default, Readline starts up in Emacs editing mode, where
the keystrokes are most similar to Emacs. This variable can be
set to either emacs or vi.
horizontal-scroll-mode
On or Off. Setting it
to On means that the text of the lines that you edit will scroll
horizontally on a single screen line when they are longer than the width
of the screen, instead of wrapping onto a new screen line. By default,
this variable is set to Off.
mark-modified-lines
On, says to display an asterisk
(`*') at the start of history lines which have been modified.
This variable is off by default.
bell-style
none, Readline never rings the bell. If set to
visible, Readline uses a visible bell if one is available.
If set to audible (the default), Readline attempts to ring
the terminal's bell.
comment-begin
vi-comment command is executed. The default value
is "#".
meta-flag
on, Readline will enable eight-bit input (it
will not strip the eighth bit from the characters it reads),
regardless of what the terminal claims it can support. The
default value is off.
convert-meta
on, Readline will convert characters with the
eigth bit set to an ASCII key sequence by stripping the eigth
bit and prepending an ESC character, converting them to a
meta-prefixed key sequence. The default value is on.
output-meta
on, Readline will display characters with the
eighth bit set directly rather than as a meta-prefixed escape
sequence. The default is off.
completion-query-items
100.
keymap
keymap names are
emacs,
emacs-standard,
emacs-meta,
emacs-ctlx,
vi,
vi-move,
vi-command, and
vi-insert.
vi is equivalent to vi-command; emacs is
equivalent to emacs-standard. The default value is emacs.
The value of the editing-mode variable also affects the
default keymap.
show-all-if-ambiguous
on,
words which have more than one possible completion cause the
matches to be listed immediately instead of ringing the bell.
The default value is off.
expand-tilde
on, tilde expansion is performed when Readline
attempts word completion. The default is off.
Control-u: universal-argument Meta-Rubout: backward-kill-word Control-o: ">&output"In the above example, `C-u' is bound to the function
universal-argument, and `C-o' is bound to run the macro
expressed on the right hand side (that is, to insert the text
`>&output' into the line).
"\C-u": universal-argument "\C-x\C-r": re-read-init-file "\e[11~": "Function Key 1"In the above example, `C-u' is bound to the function
universal-argument (just as it was in the first example),
`C-x C-r' is bound to the function re-read-init-file, and
`ESC [ 1 1 ~' is bound to insert the text `Function Key 1'.
The following escape sequences are available when specifying key
sequences:
\C-
\M-
\e
\\
\"
\'
"\C-x\\": "\\"
Readline implements a facility similar in spirit to the conditional compilation features of the C preprocessor which allows key bindings and variable settings to be performed as the result of tests. There are three parser directives used.
$if
$if construct allows bindings to be made based on the
editing mode, the terminal being used, or the application using
Readline. The text of the test extends to the end of the line;
no characters are required to isolate it.
mode
mode= form of the $if directive is used to test
whether Readline is in emacs or vi mode.
This may be used in conjunction
with the `set keymap' command, for instance, to set bindings in
the emacs-standard and emacs-ctlx keymaps only if
Readline is starting out in emacs mode.
term
term= form may be used to include terminal-specific
key bindings, perhaps to bind the key sequences output by the
terminal's function keys. The word on the right side of the
`=' is tested against the full name of the terminal and the
portion of the terminal name before the first `-'. This
allows sun to match both sun and sun-cmd,
for instance.
application
$if bash # Quote the current or previous word "\C-xq": "\eb\"\ef\"" $endif
$endif
$if command.
$else
$if directive are executed if
the test fails.
beginning-of-line (C-a)
end-of-line (C-e)
forward-char (C-f)
backward-char (C-b)
forward-word (M-f)
backward-word (M-b)
clear-screen (C-l)
redraw-current-line ()
accept-line (Newline, Return)
HISTCONTROL variable. If this line was a history
line, then restore the history line to its original state.
previous-history (C-p)
next-history (C-n)
beginning-of-history (M-<)
end-of-history (M->)
reverse-search-history (C-r)
forward-search-history (C-s)
non-incremental-reverse-search-history (M-p)
non-incremental-forward-search-history (M-n)
history-search-forward ()
history-search-backward ()
yank-nth-arg (M-C-y)
yank-last-arg (M-., M-_)
yank-nth-arg.
delete-char (C-d)
backward-delete-char (Rubout)
quoted-insert (C-q, C-v)
tab-insert (M-TAB)
self-insert (a, b, A, 1, !, ...)
transpose-chars (C-t)
transpose-words (M-t)
upcase-word (M-u)
downcase-word (M-l)
capitalize-word (M-c)
kill-line (C-k)
backward-kill-line (C-x Rubout)
unix-line-discard (C-u)
kill-whole-line ()
kill-word (M-d)
forward-word.
backward-kill-word (M-DEL)
backward-word.
unix-word-rubout (C-w)
delete-horizontal-space ()
yank (C-y)
yank-pop (M-y)
digit-argument (M-0, M-1, ... M--)
universal-argument ()
complete (TAB)
possible-completions (M-?)
insert-completions ()
possible-completions. By default, this
is not bound to a key.
start-kbd-macro (C-x ()
end-kbd-macro (C-x ))
call-last-kbd-macro (C-x e)
re-read-init-file (C-x C-r)
abort (C-g)
bell-style).
do-uppercase-version (M-a, M-b, ...)
prefix-meta (ESC)
undo (C-_, C-x C-u)
revert-line (M-r)
undo
command enough times to get back to the beginning.
tilde-expand (M-~)
dump-functions ()
display-shell-version (C-x C-v)
shell-expand-line (M-C-e)
history-expand-line (M-^)
insert-last-argument (M-., M-_)
yank-last-arg.
operate-and-get-next (C-o)
emacs-editing-mode (C-e)
vi editing mode, this causes a switch back to
emacs editing mode, as if the command set -o emacs had
been executed.
While the Readline library does not have a full set of vi
editing functions, it does contain enough to allow simple editing
of the line. The Readline vi mode behaves as specified in
the Posix 1003.2 standard.
In order to switch interactively between Emacs and Vi
editing modes, use the set -o emacs and set -o vi
commands (see section The Set Builtin).
The Readline default is emacs mode.
When you enter a line in vi mode, you are already placed in
`insertion' mode, as if you had typed an `i'. Pressing ESC
switches you into `command' mode, where you can edit the text of the
line with the standard vi movement keys, move to previous
history lines with `k', and following lines with `j', and
so forth.
This document was generated on 24 July 1996 using the texi2html translator version 1.50.