man cd
NAME
bash, :, ., [, alias, bg, bind, break, builtin, caller, cd, command,
compgen, complete, compopt, continue, declare, dirs, disown, echo,
enable, eval, exec, exit, export, false, fc, fg, getopts, hash, help,
history, jobs, kill, let, local, logout, mapfile, popd, printf, pushd,
pwd, read, readonly, return, set, shift, shopt, source, suspend, test,
times, trap, true, type, typeset, ulimit, umask, unalias, unset, wait -
bash built-in commands, see bash(1)
BASH BUILTIN COMMANDS
Unless otherwise noted, each builtin command documented in this section
as accepting options preceded by - accepts -- to signify the end of the
options. The :, true, false, and test builtins do not accept options
and do not treat -- specially. The exit, logout, break, continue, let,
and shift builtins accept and process arguments beginning with - with-
out requiring --. Other builtins that accept arguments but are not
specified as accepting options interpret arguments beginning with - as
invalid options and require -- to prevent this interpretation.
: [arguments]
No effect; the command does nothing beyond expanding arguments
and performing any specified redirections. A zero exit code is
returned.
. filename [arguments]
source filename [arguments]
Read and execute commands from filename in the current shell
environment and return the exit status of the last command exe-
cuted from filename. If filename does not contain a slash, file
names in PATH are used to find the directory containing file-
name. The file searched for in PATH need not be executable.
When bash is not in posix mode, the current directory is
searched if no file is found in PATH. If the sourcepath option
to the shopt builtin command is turned off, the PATH is not
searched. If any arguments are supplied, they become the posi-
tional parameters when filename is executed. Otherwise the
positional parameters are unchanged. The return status is the
status of the last command exited within the script (0 if no
commands are executed), and false if filename is not found or
cannot be read.
alias [-p] [name[=value] ...]
Alias with no arguments or with the -p option prints the list of
aliases in the form alias name=value on standard output. When
arguments are supplied, an alias is defined for each name whose
value is given. A trailing space in value causes the next word
to be checked for alias substitution when the alias is expanded.
For each name in the argument list for which no value is sup-
plied, the name and value of the alias is printed. Alias
returns true unless a name is given for which no alias has been
defined.
bg [jobspec ...]
Resume each suspended job jobspec in the background, as if it
had been started with &. If jobspec is not present, the shell’s
notion of the current job is used. bg jobspec returns 0 unless
run when job control is disabled or, when run with job control
enabled, any specified jobspec was not found or was started
without job control.
bind [-m keymap] [-lpsvPSV]
bind [-m keymap] [-q function] [-u function] [-r keyseq]
bind [-m keymap] -f filename
bind [-m keymap] -x keyseq:shell-command
bind [-m keymap] keyseq:function-name
bind readline-command
Display current readline key and function bindings, bind a key
sequence to a readline function or macro, or set a readline
variable. Each non-option argument is a command as it would
appear in .inputrc, but each binding or command must be passed
as a separate argument; e.g., ’"\C-x\C-r": re-read-init-file’.
Options, if supplied, have the following meanings:
-m keymap
Use keymap as the keymap to be affected by the subsequent
bindings. Acceptable keymap names are emacs, emacs-stan-
dard, 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 List the names of all readline functions.
-p Display readline function names and bindings in such a
way that they can be re-read.
-P List current readline function names and bindings.
-s Display readline key sequences bound to macros and the
strings they output in such a way that they can be re-
read.
-S Display readline key sequences bound to macros and the
strings they output.
-v Display readline variable names and values in such a way
that they can be re-read.
-V List current readline variable names and values.
-f filename
Read key bindings from filename.
-q function
Query about which keys invoke the named function.
-u function
Unbind all keys bound to the named function.
-r keyseq
Remove any current binding for keyseq.
-x keyseq:shell-command
Cause shell-command to be executed whenever keyseq is
entered. When shell-command is executed, the shell sets
the READLINE_LINE variable to the contents of the read-
line line buffer and the READLINE_POINT variable to the
current location of the insertion point. If the executed
command changes the value of READLINE_LINE or READ-
LINE_POINT, those new values will be reflected in the
editing state.
The return value is 0 unless an unrecognized option is given or
an error occurred.
break [n]
Exit from within a for, while, until, or select loop. If n is
specified, break n levels. n must be ≥ 1. If n is greater than
the number of enclosing loops, all enclosing loops are exited.
The return value is non-zero when n is ≤ 0; Otherwise, break
returns 0 value.
builtin shell-builtin [arguments]
Execute the specified shell builtin, passing it arguments, and
return its exit status. This is useful when defining a function
whose name is the same as a shell builtin, retaining the func-
tionality of the builtin within the function. The cd builtin is
commonly redefined this way. The return status is false if
shell-builtin is not a shell builtin command.
caller [expr]
Returns the context of any active subroutine call (a shell func-
tion or a script executed with the . or source builtins. With-
out expr, caller displays the line number and source filename of
the current subroutine call. If a non-negative integer is sup-
plied as expr, caller displays the line number, subroutine name,
and source file corresponding to that position in the current
execution call stack. This extra information may be used, for
example, to print a stack trace. The current frame is frame 0.
The return value is 0 unless the shell is not executing a sub-
routine call or expr does not correspond to a valid position in
the call stack.
cd [-L|-P] [dir]
Change the current directory to dir. The variable HOME is the
default dir. The variable CDPATH defines the search path for
the directory containing dir. Alternative directory names in
CDPATH are separated by a colon (:). A null directory name in
CDPATH is the same as the current directory, i.e., ‘‘.’’. If
dir begins with a slash (/), then CDPATH is not used. The -P
option says to use the physical directory structure instead of
following symbolic links (see also the -P option to the set
builtin command); the -L option forces symbolic links to be fol-
lowed. An argument of - is equivalent to $OLDPWD. If a non-
empty directory name from CDPATH is used, or if - is the first
argument, and the directory change is successful, the absolute
pathname of the new working directory is written to the standard
output. The return value is true if the directory was success-
fully changed; false otherwise.
command [-pVv] command [arg ...]
Run command with args suppressing the normal shell function
lookup. Only builtin commands or commands found in the PATH are
executed. If the -p option is given, the search for command is
performed using 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 displayed; the -V option produces a
more verbose description. If the -V or -v option is supplied,
the exit status is 0 if command was found, and 1 if not. If
neither option is supplied and an error occurred or command can-
not be found, the exit status is 127. Otherwise, the exit sta-
tus of the command builtin is the exit status of command.
compgen [option] [word]
Generate possible completion matches for word according to the
options, which may be any option accepted by the complete
builtin with the exception of -p and -r, and write the matches
to the standard output. When using the -F or -C options, the
various shell variables set by the programmable completion
facilities, while available, will not have useful values.
The matches will be generated in the same way as if the pro-
grammable completion code had generated them directly from a
completion specification with the same flags. If word is speci-
fied, only those completions matching word will be displayed.
The return value is true unless an invalid option is supplied,
or no matches were generated.
complete [-abcdefgjksuv] [-o comp-option] [-DE] [-A action] [-G glob-
pat] [-W wordlist] [-F function] [-C command]
[-X filterpat] [-P prefix] [-S suffix] name [name ...]
complete -pr [-DE] [name ...]
Specify how arguments to each name should be completed. If the
-p option is supplied, or if no options are supplied, existing
completion specifications are printed in a way that allows them
to be reused as input. The -r option removes a completion spec-
ification for each name, or, if no names are supplied, all com-
pletion specifications. The -D option indicates that the
remaining options and actions should apply to the ‘‘default’’
command completion; that is, completion attempted on a command
for which no completion has previously been defined. The -E
option indicates that the remaining options and actions should
apply to ‘‘empty’’ command completion; that is, completion
attempted on a blank line.
The process of applying these completion specifications when
word completion is attempted is described above under Pro-
grammable Completion.
Other options, if specified, have the following meanings. The
arguments to the -G, -W, and -X options (and, if necessary, the
-P and -S options) should be quoted to protect them from expan-
sion before the complete builtin is invoked.
-o comp-option
The comp-option controls several aspects of the comp-
spec’s behavior beyond the simple generation of comple-
tions. comp-option may be one of:
bashdefault
Perform the rest of the default bash completions
if the compspec generates no matches.
default Use readline’s default filename completion if
the compspec generates no matches.
dirnames
Perform directory name completion if the comp-
spec generates no matches.
filenames
Tell readline that the compspec generates file-
names, so it can perform any filename-specific
processing (like adding a slash to directory
names, quoting special characters, or suppress-
ing trailing spaces). Intended to be used with
shell functions.
nospace Tell readline not to append a space (the
default) to words completed at the end of the
line.
plusdirs
After any matches defined by the compspec are
generated, directory name completion is
attempted and any matches are added to the
results of the other actions.
-A action
The action may be one of the following to generate a
list of possible completions:
alias Alias names. May also be specified as -a.
arrayvar
Array variable names.
binding Readline key binding names.
builtin Names of shell builtin commands. May also be
specified as -b.
command Command names. May also be specified as -c.
directory
Directory names. May also be specified as -d.
disabled
Names of disabled shell builtins.
enabled Names of enabled shell builtins.
export Names of exported shell variables. May also be
specified as -e.
file File names. May also be specified as -f.
function
Names of shell functions.
group Group names. May also be specified as -g.
helptopic
Help topics as accepted by the help builtin.
hostname
Hostnames, as taken from the file specified by
the HOSTFILE shell variable.
job Job names, if job control is active. May also
be specified as -j.
keyword Shell reserved words. May also be specified as
-k.
running Names of running jobs, if job control is active.
service Service names. May also be specified as -s.
setopt Valid arguments for the -o option to the set
builtin.
shopt Shell option names as accepted by the shopt
builtin.
signal Signal names.
stopped Names of stopped jobs, if job control is active.
user User names. May also be specified as -u.
variable
Names of all shell variables. May also be spec-
ified as -v.
-G globpat
The pathname expansion pattern globpat is expanded to
generate the possible completions.
-W wordlist
The wordlist is split using the characters in the IFS
special variable as delimiters, and each resultant word
is expanded. The possible completions are the members
of the resultant list which match the word being com-
pleted.
-C command
command is executed in a subshell environment, and its
output is used as the possible completions.
-F function
The shell function function is executed in the current
shell environment. When it finishes, the possible com-
pletions are retrieved from the value of the COMPREPLY
array variable.
-X filterpat
filterpat is a pattern as used for pathname expansion.
It is applied to the list of possible completions gener-
ated by the preceding options and arguments, and each
completion matching filterpat is removed from the list.
A leading ! in filterpat negates the pattern; in this
case, any completion not matching filterpat is removed.
-P prefix
prefix is added at the beginning of each possible com-
pletion after all other options have been applied.
-S suffix
suffix is appended to each possible completion after all
other options have been applied.
The return value is true unless an invalid option is supplied,
an option other than -p or -r is supplied without a name argu-
ment, an attempt is made to remove a completion specification
for a name for which no specification exists, or an error occurs
adding a completion specification.
compopt [-o option] [-DE] [+o option] [name]
Modify completion options for each name according to the
options, or for the currently-execution completion if no names
are supplied. If no options are given, display the completion
options for each name or the current completion. The possible
values of option are those valid for the complete builtin
described above. The -D option indicates that the remaining
options should apply to the ‘‘default’’ command completion; that
is, completion attempted on a command for which no completion
has previously been defined. The -E option indicates that the
remaining options should apply to ‘‘empty’’ command completion;
that is, completion attempted on a blank line.
The return value is true unless an invalid option is supplied, an
attempt is made to modify the options for a name for which no comple-
tion specification exists, or an output error occurs.
continue [n]
Resume the next iteration of the enclosing for, while, until, or
select loop. If n is specified, resume at the nth enclosing
loop. n must be ≥ 1. If n is greater than the number of
enclosing loops, the last enclosing loop (the ‘‘top-level’’
loop) is resumed. When continue is executed inside of loop, the
return value is non-zero when n is ≤ 0; Otherwise, continue
returns 0 value. When continue is executed outside of loop, the
return value is 0.
declare [-aAfFilrtux] [-p] [name[=value] ...]
typeset [-aAfFilrtux] [-p] [name[=value] ...]
Declare variables and/or give them attributes. If no names are
given then display the values of variables. The -p option will
display the attributes and values of each name. When -p is used
with name arguments, additional options are ignored. When -p is
supplied without name arguments, it will display the attributes
and values of all variables having the attributes specified by
the additional options. If no other options are supplied with
-p, declare will display the attributes and values of all shell
variables. The -f option will restrict the display to shell
functions. The -F option inhibits the display of function defi-
nitions; only the function name and attributes are printed. If
the extdebug shell option is enabled using shopt, the source
file name and line number where the function is defined are dis-
played as well. The -F option implies -f. The following
options can be used to restrict output to variables with the
specified attribute or to give variables attributes:
-a Each name is an indexed array variable (see Arrays
above).
-A Each name is an associative array variable (see Arrays
above).
-f Use function names only.
-i The variable is treated as an integer; arithmetic evalua-
tion (see ARITHMETIC EVALUATION above) is performed when
the variable is assigned a value.
-l When the variable is assigned a value, all upper-case
characters are converted to lower-case. The upper-case
attribute is disabled.
-r Make names readonly. These names cannot then be assigned
values by subsequent assignment statements or unset.
-t Give each name the trace attribute. Traced functions
inherit the DEBUG and RETURN traps from the calling
shell. The trace attribute has no special meaning for
variables.
-u When the variable is assigned a value, all lower-case
characters are converted to upper-case. The lower-case
attribute is disabled.
-x Mark names for export to subsequent commands via the
environment.
Using ‘+’ instead of ‘-’ turns off the attribute instead, with
the exceptions that +a may not be used to destroy an array vari-
able and +r will not remove the readonly attribute. When used
in a function, makes each name local, as with the local command.
If a variable name is followed by =value, the value of the vari-
able is set to value. The return value is 0 unless an invalid
option is encountered, an attempt is made to define a function
using ‘‘-f foo=bar’’, an attempt is made to assign a value to a
readonly variable, an attempt is made to assign a value to an
array variable without using the compound assignment syntax (see
Arrays above), one of the names is not a valid shell variable
name, an attempt is made to turn off readonly status for a read-
only variable, an attempt is made to turn off array status for
an array variable, or an attempt is made to display a non-exis-
tent function with -f.
dirs [+n] [-n] [-cplv]
Without options, displays the list of currently remembered
directories. The default display is on a single line with
directory names separated by spaces. Directories are added to
the list with the pushd command; the popd command removes
entries from the list.
+n Displays the nth entry counting from the left of the list
shown by dirs when invoked without options, starting with
zero.
-n Displays the nth entry counting from the right of the
list shown by dirs when invoked without options, starting
with zero.
-c Clears the directory stack by deleting all of the
entries.
-l Produces a longer listing; the default listing format
uses a tilde to denote the home directory.
-p Print the directory stack with one entry per line.
-v Print the directory stack with one entry per line, pre-
fixing each entry with its index in the stack.
The return value is 0 unless an invalid option is supplied or n
indexes beyond the end of the directory stack.
disown [-ar] [-h] [jobspec ...]
Without options, each jobspec is removed from the table of
active jobs. If jobspec is not present, and neither -a nor -r
is supplied, the shell’s notion of the current job is used. If
the -h option is given, each jobspec is not removed from the ta-
ble, but is marked so that SIGHUP is not sent to the job if the
shell receives a SIGHUP. If no jobspec is present, and neither
the -a nor the -r option is supplied, the current job is used.
If no jobspec is supplied, the -a option means to remove or mark
all jobs; the -r option without a jobspec argument restricts
operation to running jobs. The return value is 0 unless a job-
spec does not specify a valid job.
echo [-neE] [arg ...]
Output the args, separated by spaces, followed by a newline.
The return status is always 0. If -n is specified, the trailing
newline is suppressed. If the -e option is given, interpreta-
tion of the following backslash-escaped characters is enabled.
The -E option disables the interpretation of these escape char-
acters, even on systems where they are interpreted by default.
The xpg_echo shell option may be used to dynamically determine
whether or not echo expands these escape characters by default.
echo does not interpret -- to mean the end of options. echo
interprets the following escape sequences:
\a alert (bell)
\b backspace
\c suppress further output
\e an escape character
\f form feed
\n new line
\r carriage return
\t horizontal tab
\v vertical tab
\\ backslash
\0nnn the eight-bit character whose value is the octal value
nnn (zero to three octal digits)
\xHH the eight-bit character whose value is the hexadecimal
value HH (one or two hex digits)
enable [-a] [-dnps] [-f filename] [name ...]
Enable and disable builtin shell commands. Disabling a builtin
allows a disk command which has the same name as a shell builtin
to be executed without specifying a full pathname, even though
the shell normally searches for builtins before disk commands.
If -n is used, each name is disabled; otherwise, names are
enabled. For example, to use the test binary found via the PATH
instead of the shell builtin version, run ‘‘enable -n test’’.
The -f option means to load the new builtin command name from
shared object filename, on systems that support dynamic loading.
The -d option will delete a builtin previously loaded with -f.
If no name arguments are given, or if the -p option is supplied,
a list of shell builtins is printed. With no other option argu-
ments, the list consists of all enabled shell builtins. If -n
is supplied, only disabled builtins are printed. If -a is sup-
plied, the list printed includes all builtins, with an indica-
tion of whether or not each is enabled. If -s is supplied, the
output is restricted to the POSIX special builtins. The return
value is 0 unless a name is not a shell builtin or there is an
error loading a new builtin from a shared object.
eval [arg ...]
The args are read and concatenated together into a single com-
mand. This command is then read and executed by the shell, and
its exit status is returned as the value of eval. If there are
no args, or only null arguments, eval returns 0.
exec [-cl] [-a name] [command [arguments]]
If command is specified, it replaces the shell. No new process
is created. The arguments become the arguments to command. If
the -l option is supplied, the shell places a dash at the begin-
ning of the zeroth argument passed to command. This is what
login(1) does. The -c option causes command to be executed with
an empty environment. If -a is supplied, the shell passes name
as the zeroth argument to the executed command. If command can-
not be executed for some reason, a non-interactive shell exits,
unless the shell option execfail is enabled, in which case it
returns failure. An interactive shell returns failure if the
file cannot be executed. If command is not specified, any redi-
rections take effect in the current shell, and the return status
is 0. If there is a redirection error, the return status is 1.
exit [n]
Cause the shell to exit with a status of n. If n is omitted,
the exit status is that of the last command executed. A trap on
EXIT is executed before the shell terminates.
export [-fn] [name[=word]] ...
export -p
The supplied names are marked for automatic export to the envi-
ronment of subsequently executed commands. If the -f option is
given, the names refer to functions. If no names are given, or
if the -p option is supplied, a list of all names that are
exported in this shell is printed. The -n option causes the
export property to be removed from each name. If a variable
name is followed by =word, the value of the variable is set to
word. export returns an exit status of 0 unless an invalid
option is encountered, one of the names is not a valid shell
variable name, or -f is supplied with a name that is not a func-
tion.
fc [-e ename] [-lnr] [first] [last]
fc -s [pat=rep] [cmd]
Fix Command. In the first form, a range of commands from first
to last is selected from the history list. First and last may
be specified as a string (to locate the last 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 cur-
rent command number). If last is not specified it is set to the
current command for listing (so that ‘‘fc -l -10’’ prints the
last 10 commands) and to first otherwise. If first is not spec-
ified it is set to the previous command for editing and -16 for
listing.
The -n option suppresses the command numbers when listing. The
-r option reverses the order of the commands. If the -l option
is given, the commands are listed on standard output. Other-
wise, the editor given by ename is invoked on a file containing
those commands. If ename is not given, the value of the FCEDIT
variable is used, and the value of EDITOR if FCEDIT is not set.
If neither variable is set, is used. When editing is complete,
the edited commands are echoed and executed.
In the second form, command is re-executed after each instance
of pat is replaced by rep. A useful alias to use with this is
‘‘r="fc -s"’’, so that typing ‘‘r cc’’ runs the last command
beginning with ‘‘cc’’ and typing ‘‘r’’ re-executes the last com-
mand.
If the first form is used, the return value is 0 unless an
invalid option is encountered or first or last specify history
lines out of range. If the -e option is supplied, the return
value is the value of the last command executed or failure if an
error occurs with the temporary file of commands. If the second
form is used, the return status is that of the command re-exe-
cuted, unless cmd does not specify a valid history line, in
which case fc returns failure.
fg [jobspec]
Resume jobspec in the foreground, and make it the current job.
If jobspec is not present, the shell’s notion of the current job
is used. The return value is that of the command placed into
the foreground, or failure if run when job control is disabled
or, when run with job control enabled, if jobspec does not spec-
ify a valid job or jobspec specifies a job that was started
without job control.
getopts optstring name [args]
getopts is used by shell procedures to parse positional parame-
ters. optstring contains the option characters to be recog-
nized; if a character is followed by a colon, the option is
expected to have an argument, which should be separated from it
by white space. The colon and question mark characters may not
be used as option characters. Each time it is invoked, getopts
places the next option in the shell variable name, initializing
name if it does not exist, and the index of the next argument to
be processed into the variable OPTIND. OPTIND is initialized to
1 each time the shell or a shell script is invoked. When an
option requires an argument, getopts places that argument into
the variable OPTARG. The shell does not reset OPTIND automati-
cally; it must be manually reset between multiple calls to
getopts within the same shell invocation if a new set of parame-
ters is to be used.
When the end of options is encountered, getopts exits with a
return value greater than zero. OPTIND is set to the index of
the first non-option argument, and name is set to ?.
getopts normally parses the positional parameters, but if more
arguments are given in args, getopts parses those instead.
getopts can report errors in two ways. If the first character
of optstring is a colon, silent error reporting is used. In
normal operation diagnostic messages are printed when invalid
options or missing option arguments are encountered. If the
variable OPTERR is set to 0, no error messages will be dis-
played, even if the first character of optstring is not a colon.
If an invalid option is seen, getopts places ? into name and, if
not silent, prints an error message and unsets OPTARG. If
getopts is silent, the option character found is placed in
OPTARG and no diagnostic message is printed.
If a required argument is not found, and getopts is not silent,
a question mark (?) is placed in name, OPTARG is unset, and a
diagnostic message is printed. If getopts is silent, then a
colon (:) is placed in name and OPTARG is set to the option
character found.
getopts returns true if an option, specified or unspecified, is
found. It returns false if the end of options is encountered or
an error occurs.
hash [-lr] [-p filename] [-dt] [name]
For each name, the full file name of the command is determined
by searching the directories in $PATH and remembered. If the -p
option is supplied, no path search is performed, and filename is
used as the full file name of the command. The -r option causes
the shell to forget all remembered locations. The -d option
causes the shell to forget the remembered location of each name.
If the -t option is supplied, the full pathname to which each
name corresponds is printed. If multiple name arguments are
supplied with -t, the name is printed before the hashed full
pathname. The -l option causes output to be displayed in a for-
mat that may be reused as input. If no arguments are given, or
if only -l is supplied, information about remembered commands is
printed. The return status is true unless a name is not found
or an invalid option is supplied.
help [-dms] [pattern]
Display helpful information about builtin commands. If pattern
is specified, help gives detailed help on all commands matching
pattern; otherwise help for all the builtins and shell control
structures is printed.
-d Display a short description of each pattern
-m Display the description of each pattern in a manpage-like
format
-s Display only a short usage synopsis for each pattern
The return status is 0 unless no command matches pattern.
history [n]
history -c
history -d offset
history -anrw [filename]
history -p arg [arg ...]
history -s arg [arg ...]
With no options, display the command history list with line num-
bers. Lines listed with a * have been modified. An argument of
n lists only the last n lines. If the shell variable HISTTIME-
FORMAT is set and not null, it is used as a format string for
strftime(3) to display the time stamp associated with each dis-
played history entry. No intervening blank is printed between
the formatted time stamp and the history line. If filename is
supplied, it is used as the name of the history file; if not,
the value of HISTFILE is used. Options, if supplied, have the
following meanings:
-c Clear the history list by deleting all the entries.
-d offset
Delete the history entry at position offset.
-a Append the ‘‘new’’ history lines (history lines entered
since the beginning of the current bash session) to the
history file.
-n 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.
-r Read the contents of the history file and use them as the
current history.
-w Write the current history to the history file, overwrit-
ing the history file’s contents.
-p Perform history substitution on the following args and
display the result on the standard output. Does not
store the results in the history list. Each arg must be
quoted to disable normal history expansion.
-s Store the args in the history list as a single entry.
The last command in the history list is removed before
the args are added.
If the HISTTIMEFORMAT variable is set, the time stamp informa-
tion associated with each history entry is written to the his-
tory file, marked with the history comment character. When the
history file is read, lines beginning with the history comment
character followed immediately by a digit are interpreted as
timestamps for the previous history line. The return value is 0
unless an invalid option is encountered, an error occurs while
reading or writing the history file, an invalid offset is sup-
plied as an argument to -d, or the history expansion supplied as
an argument to -p fails.
jobs [-lnprs] [ jobspec ... ]
jobs -x command [ args ... ]
The first form lists the active jobs. The options have the fol-
lowing meanings:
-l List process IDs in addition to the normal information.
-p List only the process ID of the job’s process group
leader.
-n Display information only about jobs that have changed
status since the user was last notified of their status.
-r Restrict output to running jobs.
-s Restrict output to stopped jobs.
If jobspec is given, output is restricted to information about
that job. The return status is 0 unless an invalid option is
encountered or an invalid jobspec is supplied.
If the -x option is supplied, jobs replaces any jobspec found in
command or args with the corresponding process group ID, and
executes command passing it args, returning its exit status.
kill [-s sigspec | -n signum | -sigspec] [pid | jobspec] ...
kill -l [sigspec | exit_status]
Send the signal named by sigspec or signum to the processes
named by pid or jobspec. sigspec is either a case-insensitive
signal name such as SIGKILL (with or without the SIG prefix) or
a signal number; signum is a signal number. If sigspec is not
present, then SIGTERM is assumed. An argument of -l lists the
signal names. If any arguments are supplied when -l is given,
the names of the signals corresponding to the arguments are
listed, and the return status is 0. The exit_status argument to
-l is a number specifying either a signal number or the exit
status of a process terminated by a signal. kill returns true
if at least one signal was successfully sent, or false if an
error occurs or an invalid option is encountered.
let arg [arg ...]
Each arg is an arithmetic expression to be evaluated (see ARITH-
METIC EVALUATION above). If the last arg evaluates to 0, let
returns 1; 0 is returned otherwise.
local [option] [name[=value] ...]
For each argument, a local variable named name is created, and
assigned value. The option can be any of the options accepted
by declare. When local is used within a function, it causes the
variable name to have a visible scope restricted to that func-
tion and its children. With no operands, local writes a list of
local variables to the standard output. It is an error to use
local when not within a function. The return status is 0 unless
local is used outside a function, an invalid name is supplied,
or name is a readonly variable.
logout Exit a login shell.
mapfile [-n count] [-O origin] [-s count] [-t] [-u fd] [-C callback]
[-c quantum] [array]
readarray [-n count] [-O origin] [-s count] [-t] [-u fd] [-C callback]
[-c quantum] [array]
Read lines from the standard input into the indexed array vari-
able array, or from file descriptor fd if the -u option is sup-
plied. The variable MAPFILE is the default array. Options, if
supplied, have the following meanings:
-n Copy at most count lines. If count is 0, all lines are
copied.
-O Begin assigning to array at index origin. The default
index is 0.
-s Discard the first count lines read.
-t Remove a trailing newline from each line read.
-u Read lines from file descriptor fd instead of the stan-
dard input.
-C Evaluate callback each time quantum lines are read. The
-c option specifies quantum.
-c Specify the number of lines read between each call to
callback.
If -C is specified without -c, the default quantum is 5000.
When callback is evaluated, it is supplied the index of the next
array element to be assigned as an additional argument. call-
back is evaluated after the line is read but before the array
element is assigned.
If not supplied with an explicit origin, mapfile will clear
array before assigning to it.
mapfile returns successfully unless an invalid option or option
argument is supplied, array is invalid or unassignable, or if
array is not an indexed array.
popd [-n] [+n] [-n]
Removes entries from the directory stack. With no arguments,
removes the top directory from the stack, and performs a cd to
the new top directory. Arguments, if supplied, have the follow-
ing meanings:
-n Suppresses the normal change of directory when removing
directories from the stack, so that only the stack is
manipulated.
+n Removes the nth entry counting from the left of the list
shown by dirs, starting with zero. For example: ‘‘popd
+0’’ removes the first directory, ‘‘popd +1’’ the second.
-n Removes the nth entry counting from the right of the list
shown by dirs, starting with zero. For example: ‘‘popd
-0’’ removes the last directory, ‘‘popd -1’’ the next to
last.
If the popd command is successful, a dirs is performed as well,
and the return status is 0. popd returns false if an invalid
option is encountered, the directory stack is empty, a non-exis-
tent directory stack entry is specified, or the directory change
fails.
printf [-v var] format [arguments]
Write the formatted arguments to the standard output under the
control of the format. The format is a character string which
contains three types of objects: plain characters, which are
simply copied to standard output, character escape sequences,
which are converted and copied to the standard output, and for-
mat specifications, each of which causes printing of the next
successive argument. In addition to the standard printf(1) for-
mats, %b causes printf to expand backslash escape sequences in
the corresponding argument (except that \c terminates output,
backslashes in \', \", and \? are not removed, and octal escapes
beginning with \0 may contain up to four digits), and %q causes
printf to output the corresponding argument in a format that can
be reused as shell input.
The -v option causes the output to be assigned to the variable
var rather than being printed to the standard output.
The format is reused as necessary to consume all of the argu-
ments. If the format requires more arguments than are supplied,
the extra format specifications behave as if a zero value or
null string, as appropriate, had been supplied. The return
value is zero on success, non-zero on failure.
pushd [-n] [+n] [-n]
pushd [-n] [dir]
Adds a directory to the top of the directory stack, or rotates
the stack, making the new top of the stack the current working
directory. With no arguments, exchanges the top two directories
and returns 0, unless the directory stack is empty. Arguments,
if supplied, have the following meanings:
-n Suppresses the normal change of directory when adding
directories to the stack, so that only the stack is
manipulated.
+n Rotates the stack so that the nth directory (counting
from the left of the list shown by dirs, starting with
zero) is at the top.
-n Rotates the stack so that the nth directory (counting
from the right of the list shown by dirs, starting with
zero) is at the top.
dir Adds dir to the directory stack at the top, making it the
new current working directory.
If the pushd command is successful, a dirs is performed as well.
If the first form is used, pushd returns 0 unless the cd to dir
fails. With the second form, pushd returns 0 unless the direc-
tory stack is empty, a non-existent directory stack element is
specified, or the directory change to the specified new current
directory fails.
pwd [-LP]
Print the absolute pathname of the current working directory.
The pathname printed contains no symbolic links if the -P option
is supplied or the -o physical option to the set builtin command
is enabled. If the -L option is used, the pathname printed may
contain symbolic links. The return status is 0 unless an error
occurs while reading the name of the current directory or an
invalid option is supplied.
read [-ers] [-a aname] [-d delim] [-i text] [-n nchars] [-N nchars] [-p
prompt] [-t timeout] [-u fd] [name ...]
One line is read from the standard input, or from the file
descriptor fd supplied as an argument to the -u option, and the
first word is assigned to the first name, the second word to the
second name, and so on, with leftover words and their interven-
ing separators assigned to the last name. If there are fewer
words read from the input stream than names, the remaining names
are assigned empty values. The characters in IFS are used to
split the line into words. The backslash character (\) may be
used to remove any special meaning for the next character read
下一篇: 探索getopt函数的初步理解
推荐阅读
-
MP3、AAC、WMA、Ogg、MPC、WAV、FLAC、APE、WV、CD、MIDI、RealVideo 及其他音频格式的分析和比较
-
Linux 相对路径和绝对路径、cd 命令、创建和删除目录、rm 命令
-
CMD 命令 目录命令(md、cd、rd、dir、path、tree、deltree)
-
数据库 CI/CD 工具 Bytebase 简介
-
[小型团队自动化] (II) 使用 Vault 作为凭证存储的无人机 CI -- 构建自己的 CI/CD 工作流程
-
在一篇文章中开始使用 Gitlab CI/CD
-
蓝鲸 DevOps 深度分析系列(1):蓝盾平台概述 - 蓝鲸是一个用于研发和运营的集成平台,涵盖 CI、CD、CO
-
epoll简介及触发模式(accept、read、send)-epoll的简单介绍 epoll在LT和ET模式下的读写方式 一、epoll的接口非常简单,一共就三个函数:1. int epoll_create(int size);创建一个epoll的句柄,size用来告诉内核这个监听的数目一共有多大。这个参数不同于select中的第一个参数,给出最大监听的fd+1的值。需要注意的是,当创建好epoll句柄后,它就是会占用一个fd值,在linux下如果查看/proc/进程id/fd/,是能够看到这个fd的,所以在使用完epoll后,必须调用close关闭,否则可能导致fd被耗尽。2. int epoll_ctl(int epfd, int op, int fd, struct epoll_event *event);epoll的事件注册函数,它不同与select是在监听事件时告诉内核要监听什么类型的事件,而是在这里先注册要监听的事件类型。第一个参数是epoll_create的返回值,第二个参数表示动作,用三个宏来表示:EPOLL_CTL_ADD:注册新的fd到epfd中;EPOLL_CTL_MOD:修改已经注册的fd的监听事件;EPOLL_CTL_DEL:从epfd中删除一个fd;第三个参数是需要监听的fd,第四个参数是告诉内核需要监听什么事,struct epoll_event结构如下:struct epoll_event { __uint32_t events; /* Epoll events */ epoll_data_t data; /* User data variable */};events可以是以下几个宏的集合:EPOLLIN :表示对应的文件描述符可以读(包括对端SOCKET正常关闭); EPOLLIN事件:EPOLLIN事件则只有当对端有数据写入时才会触发,所以触发一次后需要不断读取所有数据直到读完EAGAIN为止。否则剩下的数据只有在下次对端有写入时才能一起取出来了。现在明白为什么说epoll必须要求异步socket了吧?如果同步socket,而且要求读完所有数据,那么最终就会在堵死在阻塞里。 EPOLLOUT:表示对应的文件描述符可以写; EPOLLOUT事件:EPOLLOUT事件只有在连接时触发一次,表示可写,其他时候想要触发,那要先准备好下面条件:1.某次write,写满了发送缓冲区,返回错误码为EAGAIN。2.对端读取了一些数据,又重新可写了,此时会触发EPOLLOUT。简单地说:EPOLLOUT事件只有在不可写到可写的转变时刻,才会触发一次,所以叫边缘触发,这叫法没错的!其实,如果真的想强制触发一次,也是有办法的,直接调用epoll_ctl重新设置一下event就可以了,event跟原来的设置一模一样都行(但必须包含EPOLLOUT),关键是重新设置,就会马上触发一次EPOLLOUT事件。1. 缓冲区由满变空.2.同时注册EPOLLIN | EPOLLOUT事件,也会触发一次EPOLLOUT事件这个两个也会触发EPOLLOUT事件 EPOLLPRI:表示对应的文件描述符有紧急的数据可读(这里应该表示有带外数据到来);EPOLLERR:表示对应的文件描述符发生错误;EPOLLHUP:表示对应的文件描述符被挂断;EPOLLET: 将EPOLL设为边缘触发(Edge Triggered)模式,这是相对于水平触发(Level Triggered)来说的。EPOLLONESHOT:只监听一次事件,当监听完这次事件之后,如果还需要继续监听这个socket的话,需要再次把这个socket加入到EPOLL队列里3. int epoll_wait(int epfd, struct epoll_event * events, int maxevents, int timeout);等待事件的产生,类似于select调用。参数events用来从内核得到事件的集合,maxevents告之内核这个events有多大,这个maxevents的值不能大于创建epoll_create时的size,参数timeout是超时时间(毫秒,0会立即返回,-1将不确定,也有说法说是永久阻塞)。该函数返回需要处理的事件数目,如返回0表示已超时。-------------------------------------------------------------------------------------------- 从man手册中,得到ET和LT的具体描述如下EPOLL事件有两种模型:Edge Triggered (ET)Level Triggered (LT)假如有这样一个例子:1. 我们已经把一个用来从管道中读取数据的文件句柄(RFD)添加到epoll描述符2. 这个时候从管道的另一端被写入了2KB的数据3. 调用epoll_wait(2),并且它会返回RFD,说明它已经准备好读取操作4. 然后我们读取了1KB的数据5. 调用epoll_wait(2)......Edge Triggered 工作模式:如果我们在第1步将RFD添加到epoll描述符的时候使用了EPOLLET标志,那么在第5步调用epoll_wait(2)之后将有可能会挂起,因为剩余的数据还存在于文件的输入缓冲区内,而且数据发出端还在等待一个针对已经发出数据的反馈信息。只有在监视的文件句柄上发生了某个事件的时候 ET 工作模式才会汇报事件。因此在第5步的时候,调用者可能会放弃等待仍在存在于文件输入缓冲区内的剩余数据。在上面的例子中,会有一个事件产生在RFD句柄上,因为在第2步执行了一个写操作,然后,事件将会在第3步被销毁。因为第4步的读取操作没有读空文件输入缓冲区内的数据,因此我们在第5步调用 epoll_wait(2)完成后,是否挂起是不确定的。epoll工作在ET模式的时候,必须使用非阻塞套接口,以避免由于一个文件句柄的阻塞读/阻塞写操作把处理多个文件描述符的任务饿死。最好以下面的方式调用ET模式的epoll接口,在后面会介绍避免可能的缺陷。 i 基于非阻塞文件句柄 ii 只有当read(2)或者write(2)返回EAGAIN时才需要挂起,等待。但这并不是说每次read时都需要循环读,直到读到产生一个EAGAIN才认为此次事件处理完成,当read返回的读到的数据长度小于请求的数据长度时,就可以确定此时缓冲中已没有数据了,也就可以认为此事读事件已处理完成。Level Triggered 工作模式相反的,以LT方式调用epoll接口的时候,它就相当于一个速度比较快的poll(2),并且无论后面的数据是否被使用,因此他们具有同样的职能。因为即使使用ET模式的epoll,在收到多个chunk的数据的时候仍然会产生多个事件。调用者可以设定EPOLLONESHOT标志,在 epoll_wait(2)收到事件后epoll会与事件关联的文件句柄从epoll描述符中禁止掉。因此当EPOLLONESHOT设定后,使用带有 EPOLL_CTL_MOD标志的epoll_ctl(2)处理文件句柄就成为调用者必须作的事情。然后详细解释ET, LT:LT(level triggered)是缺省的工作方式,并且同时支持block和no-block socket.在这种做法中,内核告诉你一个文件描述符是否就绪了,然后你可以对这个就绪的fd进行IO操作。如果你不作任何操作,内核还是会继续通知你的,所以,这种模式编程出错误可能性要小一点。传统的select/poll都是这种模型的代表.ET(edge-triggered)是高速工作方式,只支持no-block socket。在这种模式下,当描述符从未就绪变为就绪时,内核通过epoll告诉你。然后它会假设你知道文件描述符已经就绪,并且不会再为那个文件描述符发送更多的就绪通知,直到你做了某些操作导致那个文件描述符不再为就绪状态了(比如,你在发送,接收或者接收请求,或者发送接收的数据少于一定量时导致了一个EWOULDBLOCK 错误)。但是请注意,如果一直不对这个fd作IO操作(从而导致它再次变成未就绪),内核不会发送更多的通知(only once),不过在TCP协议中,ET模式的加速效用仍需要更多的benchmark确认(这句话不理解)。在许多测试中我们会看到如果没有大量的idle -connection或者dead-connection,epoll的效率并不会比select/poll高很多,但是当我们遇到大量的idle- connection(例如WAN环境中存在大量的慢速连接),就会发现epoll的效率大大高于select/poll。(未测试)另外,当使用epoll的ET模型来工作时,当产生了一个EPOLLIN事件后,读数据的时候需要考虑的是当recv返回的大小如果等于请求的大小,那么很有可能是缓冲区还有数据未读完,也意味着该次事件还没有处理完,所以还需要再次读取: 这里只是说明思路(参考《UNIX网络编程》) while(rs) {buflen = recv(activeevents[i].data.fd, buf, sizeof(buf), 0);if(buflen < 0){// 由于是非阻塞的模式,所以当errno为EAGAIN时,表示当前缓冲区已无数据可读// 在这里就当作是该次事件已处理处.if(errno == EAGAIN)break; else return; }else if(buflen == 0) { // 这里表示对端的socket已正常关闭. } if(buflen == sizeof(buf) rs = 1; // 需要再次读取 else rs = 0; } 还有,假如发送端流量大于接收端的流量(意思是epoll所在的程序读比转发的socket要快),由于是非阻塞的socket,那么send函数虽然返回,但实际缓冲区的数据并未真正发给接收端,这样不断的读和发,当缓冲区满后会产生EAGAIN错误(参考man send),同时,不理会这次请求发送的数据.所以,需要封装socket_send的函数用来处理这种情况,该函数会尽量将数据写完再返回,返回-1表示出错。在socket_send内部,当写缓冲已满(send返回-1,且errno为EAGAIN),那么会等待后再重试.这种方式并不很完美,在理论上可能会长时间的阻塞在socket_send内部,但暂没有更好的办法. ssize_t socket_send(int sockfd, const char* buffer, size_t buflen) { ssize_t tmp; size_t total = buflen; const char *p = buffer; while(1) { tmp = send(sockfd, p, total, 0); if(tmp < 0) { // 当send收到信号时,可以继续写,但这里返回-1. if(errno == EINTR) return -1; // 当socket是非阻塞时,如返回此错误,表示写缓冲队列已满, // 在这里做延时后再重试. if(errno == EAGAIN) { usleep(1000); continue; } return -1; } if((size_t)tmp == total) return buflen; total -= tmp; p += tmp; } return tmp; } 二、epoll在LT和ET模式下的读写方式 在一个非阻塞的socket上调用read/write函数, 返回EAGAIN或者EWOULDBLOCK(注: EAGAIN就是EWOULDBLOCK) 从字面上看, 意思是: * EAGAIN: 再试一次 * EWOULDBLOCK: 如果这是一个阻塞socket, 操作将被block * perror输出: Resource temporarily unavailable 总结: 这个错误表示资源暂时不够, 可能read时, 读缓冲区没有数据, 或者, write时,写缓冲区满了 。 遇到这种情况, 如果是阻塞socket, read/write就要阻塞掉。 而如果是非阻塞socket, read/write立即返回-1, 同 时errno设置为EAGAIN. 所以, 对于阻塞socket, read/write返回-1代表网络出错了. 但对于非阻塞socket, read/write返回-1不一定网络真的出错了. 可能是Resource temporarily unavailable. 这时你应该再试, 直到Resource available. 综上, 对于non-blocking的socket, 正确的读写操作为: 读: 忽略掉errno = EAGAIN的错误, 下次继续读 写: 忽略掉errno = EAGAIN的错误, 下次继续写 对于select和epoll的LT模式, 这种读写方式是没有问题的. 但对于epoll的ET模式, 这种方式还有漏洞. epoll的两种模式 LT 和 ET
-
6 幅图表帮助您了解 CI/CD 管道
-
docker+gitea+drone 实现超轻量级 CI/CD 实践