Linux: Explore bash’s Built-In Commands

bash in Linux has more than 50 built-in commands, including common commands such as cd and pwd, as well as many others that are used infrequently. You can use these built-in commands in any bash script or at the shell prompt.

The table describes most of the bash built-in commands and their arguments. After looking through this information, type helpcommand to read more about a specific built-in command. For example, to find out more about the built-in command test, type the following:

help test

Doing so displays the following information:

test: test [expr]
Exits with a status of 0 (true) or 1 (false) depending on
the evaluation of EXPR. Expressions may be unary or binary. Unary
expressions are often used to examine the status of a file. There
are string operators as well, and numeric comparison operators.
File operators:
-a FILE True if file exists.
-b FILE True if file is block special.
-c FILE True if file is character special.
-d FILE True if file 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 is set-group-id.
-h FILE True if file is a symbolic link.
-L FILE True if file is a symbolic link.
-k FILE True if file has its 'sticky' bit set.
-p FILE True if file is a named pipe.
-r FILE True if file is readable by you.
-s FILE True if file exists and is not empty.
-S FILE True if file is a socket.
-t FD True if FD is opened on a terminal.
-u FILE True if the file is set-user-id.
-w FILE True if the file is writable by you.
-x FILE True if the file is executable by you.
-O FILE True if the file is effectively owned by you.
-G FILE True if the file is effectively owned by your group.
( … Lines deleted …)

Where necessary, the online help from the help command includes a considerable amount of detail.

Summary of Built-in Commands in bash Shell
This Function Does the Following
. filename [arguments] Reads and executes commands from the specified filename using the optional arguments. (Works the same way as the source command.)
: [arguments] Expands the arguments but does not process them.
[ expr ] Evaluates the expression expr and returns zero status if expr is true.
alias [name[=value] … ] Allows one value to equal another. For example, you could set xyz to run bg.
bg [job] Puts the specified job in the background. If no job is specified, it puts the currently executing command in the background.
break [n] Exits from a for, while, or until loop. If n is specified, the nth enclosing loop is exited.
cd [dir] Changes the current directory to dir.
command [-pVv] cmd [arg … ] Runs the command cmd with the specified arguments (ignoring any shell function named cmd).
continue [n] Starts the next iteration of the for, while, or until loop. If n is specified, the next iteration of the nth enclosing loop is started.
declare [-frxi] [name[=value]] Declares a variable with the specified name and optionally, assigns it a value.
dirs [-l] [+/-n] Displays the list of currently remembered directories.
echo [-neE] [arg … ] Displays the arguments, arg … , on standard output.
enable [-n] [-all] Enables or disables the specified built-in commands.
eval [arg … ] Concatenates the arguments, arg … , and executes them as a command.
exec [command [arguments]] Replaces the current instance of the shell with a new process that runs the specified command. with the given arguments
exit [n] Exits the shell with the status code n.
export [-nf] [name[=word]] … Defines a specified environment variable and exports it to future processes.
fc -s [pat=rep] [cmd] Re-executes the command after replacing the pattern pat with rep.
fg [jobspec] Puts the specified job, jobspec, in the foreground. If no job is specified, it puts the most recent job in the foreground.
hash [-r] [name] Remembers the full pathname of a specified command.
help [cmd … ] Displays help information for specified built-in commands, cmd… .
history [n] Displays past commands or past n commands, if you specify a number n.
jobs [-lnp] [ jobspec … ] Lists currently active jobs.
kill [-s sigspec | -sigspec] [pid | jobspec] … let arg [arg … ] Evaluates each argument and returns 1 if the last arg is 0.
local [name[=value] … ] Creates a local variable with the specified name and value (used in shell functions).
logout Exits a login shell.
popd [+/-n] Removes the specified number of entries from the directory stack.
pushd [dir] Adds a specified directory, dir, to the top of the directory stack.
pwd Prints the full pathname of the current working directory.
read [-r] [name … ] Reads a line from standard input and parses it.
readonly [-f] [name … ] Marks the specified variables as read-only so that the variables cannot be changed later.
return [n] Exits the shell function with the return value n.
set [--abefhkmnptuvxldCHP] [-o option] [arg … ] Sets various flags.
shift [n] Makes the n+1 argument $1, the n+2 argument $2, and so on.
times Prints the accumulated user and system times for processes run from the shell.
trap [-l] [cmd] [sigspec] Executes cmd when the signal sigspec is received.
type [-all] [-type |-path] name [name … ] Indicates how the shell interprets each name.
ulimit [-SHacdfmstpnuv [limit]] Controls resources available to the shell.
umask [-S] [mode] Sets the file creation mask — the default permission to the mode specified for the files.
unalias [-a] [name … ] Undefines a specified alias.
unset [-fv] [name … ] Removes the definition of specified variables.
wait [n] Waits for a specified process (n represents its PID) to terminate.

Some external programs may have the same name as bash built-in commands. If you want to run any such external program, you have to specify explicitly the full pathname of that program. Otherwise bash executes the built-in command of the same name.

  • Add a Comment
  • Print
  • Share
blog comments powered by Disqus
Advertisement

Inside Dummies.com