Bash Builtin Commands
The following commands are builtin for bash or shell. This is not
a complete list and emphasizes those that are more useful for shell
programming.
: [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]
break [n]
Exit from within a for, while, or until loop. If n
is specified, break n levels. n must be >= 1.
break
break 2
continue [n]
Resume the next iteration of the enclosing for,
while, or until loop. If n is specified, resume at the nth enclosing loop.
continue
continue 2
declare [-frxi] [name[=value]]
echo [-neE] [arg ...]
Output the args, separated by spaces. The return
status is always 0. If -n is specified, the trailing
newline is suppressed. If the -e option is
given, interpretation of the following backslash-escaped
characters is enabled. The -E option disables the
interpretation of these escape characters, even on systems where
they are interpreted by default.
\a alert (bell)
\b backspace
\c suppress trailing newline
\f form feed
\n new line
\r carriage return
\t horizontal tab
\v vertical tab
\\ backslash
\nnn the character whose ASCII code is nnn
echo "The value of name is", $NAME
echo -e "\tThis is a u with a circumflex \227 \a\a"
eval [arg ...]
exec [[-] command [arguments]]
If command is specified, it replaces the shell. No
new process is created. The arguments become the
arguments to command.
exec ls /usr/tables $3
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.
exit 0
export [-nf] [name[=word]] ...
export -p
getopts optstring name [args]
getopts is used by shell procedures to parse positional
parameters. optstring contains the option letters to be recognized;
If a letter is followed by a colon, the option is expected to have an
argument, which should be separated from it by white
space. 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 automatically; it must be manually reset
between multiple calls to getopts within the same
shell invocation if a new set of parameters is to
be used.
If you executed the following command in a script that was
called with the syntax: script3
while getopts "xy:z" o; do -y 13 -z
case $o in
x ) echo "-x is there" ;;
y ) echo "-y is there with arg $OPTARG" ;;
z ) echo "-z is there" ;;
?) echo "Usagei: script3 [-x] [-y arg] [-z] ;;
esac
done
would output:
-y is there with arg 13
-z is there
let arg [arg ...]
local [name[=value] ...]
For each argument, create a local variable named
name, and assign it value. When local is used
within a function, it causes the variable name to
have a visible scope restricted to that function
local NAME COUNT=0
read [-r] [name ...]
One line is read from the standard input, and the
first word is assigned to the first name, the second word to
the second name, and so on, with leftover words assigned
to the last name. Only the
characters in IFS are recognized as word delimiters.
If no names are supplied, the line read is
assigned to the variable REPLY. The return code is
zero, unless end-of-file is encountered.
There are several options:
-
- -r do not ignore a backslash-newline pair.
- -e use readline for input
- -s do not echo
- -t to   -   timeout after to seconds
- -a name   -   assign values to array name
- -p prompt   -   prompt user with string prompt
read -p "Enter your name" NAME
read -p "Enter your password: -s PASSWORD
read -p "Enter the dimensions" -a DIM
readonly [-f] [name ...]
readonly -p
return [n]
Causes a function to exit with the return value
specified by n. If n is omitted, the return status
is that of the last command executed in the function body.
If used outside a function, but during
execution of a script by the . (source) command,
it causes the shell to stop executing that script
return -1
set [--abefhkmnptuvxldCHP] [-o option] [arg ...]
set is the most complicated built-in function. Its purpose
is to set the properties of the shell, and since there are lots
of properties, set has lots of options. If no options are given,
it displays the names and values of the shell variables
Otherwise it sets the desired shell properties. A small subset of the
options is:
-a Automatically mark variables which are mod
ified or created for export to the environment of subsequent
commands.
-b Cause the status of terminated background
jobs to be reported immediately, rather
than before the next primary prompt.
-e Exit immediately if a simple-command
exits with a non-zero
status. The shell does not exit if the
command that fails is part of an until or
while loop, part of an if statement, part
of a && or || list, or if the command's
return value is being inverted via !.
-f Disable pathname expansion.
-h Locate and remember function commands as
functions are defined. Function commands
are normally looked up when the function is
executed.
-k All keyword arguments are placed in the
environment for a command, not just those
that precede the command name.
-m Monitor mode. Job control is enabled.
This flag is on by default for interactive
shells on systems that support it (see JOB
CONTROL above). Background processes run
in a separate process group and a line con
taining their exit status is printed upon
their completion.
-n Read commands but do not execute them.
This may be used to check a shell script
for syntax errors. This is ignored for
interactive shells.
-o option-name
The option-name can be one of the follow
ing:
allexport
Same as -a.
braceexpand
set
set -f -b
shift [n]
The positional parameters from n+1 ... are renamed
to $1 .... Parameters represented by the numbers
$# down to $#-n+1 are unset. If n is 0, no parameters
are changed. If n is not given, it is assumed
to be 1. n must be a non-negative number less than
or equal to $#. If n is greater than $#, the positional parameters
are not changed. The return status is greater than 0 if n is
greater than $# or less than 0; otherwise 0.
shift 5
test expr
[ expr ]
Return a status of 0 (true) or 1 (false) depending
on the evaluation of the conditional expression
expr. Expressions may be unary or binary. Unary
expressions are often used to examine the status of
a file. There are string operators and numeric
comparison operators as well. Each operator and
operand must be a separate argument. If file is of
the form /dev/fd/n, then file descriptor n is
checked.
-b file True if file exists and is block special.
-c file True if file exists and is character special.
-d file True if file exists and is a directory.
-e file True if file exists.
-f file True if file exists and is a regular file.
-g file True if file exists and is set-group-id.
-k file True if file has its ``sticky'' bit set.
-L file True if file exists and is a symbolic link.
-p file True if file exists and is a named pipe.
-r file True if file exists and is readable.
-s file True if file exists and has a size greater than zero.
-S file True if file exists and is a socket.
-t fd True if fd is opened on a terminal.
-u file True if file exists and its set-user-id bit is set.
-w file True if file exists and is writable.
-x file True if file exists and is executable.
-O file True if file exists and is owned by the effective user id.
-G file True if file exists and is owned by the effective group id.
file1 -nt file2 True if file1 is newer (according to modifi cation date) than file2.
file1 -ot file2 True if file1 is older than file2.
file1 -ef file True if file1 and file2 have the same device and inode numbers.
-z string True if the length of string is zero.
-n string string True if the length of string is non-zero.
string1 = string2 True if the strings are equal.
string1 != string2 True if the strings are not equal.
! expr True if expr is false.
expr1 -a expr2 True if both expr1 AND expr2 are true.
expr1 -o expr2 True if either expr1 OR expr2 is true.
arg1 OP arg2 OP is one of -eq, -ne, -lt, -le, -gt, or -ge
These arithmetic binary operators
return true if arg1 is equal, not-equal,
less-than, less-than-or-equal, greater-than,
or greater-than-or-equal than arg2, respectively. Arg1
and arg2 may be positive integers, negative integers,
or the special expression -l string, which evaluates to the
length of string.
test -e /etc/nologin
test $NAME="Dilbert"
unset [-fv] [name ...]
wait [n]
Termination status. n may be a process ID or a job
specification; if a job spec is given, all processes in that job's
pipeline are waited for. If n
is not given, all currently active child processes
are waited for, and the return status is zero. If
n specifies a non-existant process or job, the
return status is 127. Otherwise, the return status
is the exit status of the last process or job
waited for.
wait 3154