BASH / Some built-in commands

: [args]

does nothing beyond expanding args and performing any specified redirections; returns 0;

. file [args]

source file [args]

read and execute cmds from the specified file in the current shell env; if file does not contain a slash, PATH is used to find the dir containing file (which need not be executable in this case); when ~ is not in posix mode, the current dir is searched if file was not found in PATH; if sourcepath option to built-in shopt cmd is turned off, the PATH is not searched; any args supplied become positional params; the return status is the status of the last cmd executed, 0 if no cmds are executed, false if file not found or cannot be read;

alias [name[=value]...]

without args outputs the list of aliases (name=value); when args 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 arg list for which no value is supplied, name and value of the alias are printed; alias returns true unless a name is given for which no alias has been defined;

bg [jobspec]

resume the specified suspended job in the background, as if it had been started with &; if none specified, the shell's notion of the current job is used;

bind [options]

display current readline key and function bindings, bind a key sequence to a readline function or macro, set a readline variable; each non-option arg is a cmd as it would appear in .inputrc, but each binding or cmd must be passed as a separate arg; options are:

-m keymap

use keymap as the keymap to be affected by the subsequent bindings; acceptable names are: emacs (also, emacs-standard), emacs-meta, emacs-ctlx, vi (also, vi-command), vi-move, vi-insert;

-l list the names of all readline functions;
-p display readline func names and bindings in such a way that they can be re-read;
-P list current readline func names and bindings;
-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;
-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;

-f file

read key bindings from the specified file;

-q func

query which keys invoke the named function;

-u func

unbind all keys bound to the named function;

-r keyseq

remove any current binding for keyseq;

-x keyseq:shell-cmd

cause the specified shell cmd to be executed any time keyseq is entered;

The return value is 0 unless bad option or error occurred;

break [n]

exit from within for, while, until, select loop; if n is specified, break n levels; the return value is 0 unless called outside any loop;

cd [-L | -P][dir]

change current dir; the default dir is set by env var HOME, while env var CDPATH defines the search path for the dir containing dir; if dir begins with '/' CDPATH is not used; -L requires to follow symlinks, -P causes to stick to the physical dir struct; arg '' is equivalent to $OLDPWD;

continue [n]

skip the remaining statements of the enclosing for, while, until, select loop and begin next iteration; n causes to resume at the nth enclosing loop; the return value is 0 unless called outside any loop;

dirs [options][+m | -m]

displays the list of currently remembered dirs; +m | -m shows m-th entry from left / right of the list; options are:

-c clear the dir stack;
-l produce a long listing;
-p print dir stack one entry per line;
-v print dir stack one entry per line with index;

The return value is 0 unless bad option is supplied or index (+m|-m) beyond the end of the dir stack;

echo [-neE][arg ...]

output args separated by spaces, followed by a newline; the return status is always 0; -n is used to suppress the trailing newlines; -e enables interpretation of the following backslash-escaped chars; -E disables the interpretaion of these escape chars;

Backslash-escaped characters:

\aalert (bell);
\bbackspace;
\csuppress trailing newline;
\ean escape char;
\fform feed;
\nnew line;
\rcarriadge return;
\thorizontal tab;
\vvertical tab;
\\backslash;
\0nnn  8-bit char with octal value nnn;
\nnn 8-bit char with octal value nnn;
\xHH 8-bit char with hex value HH;

eval [arg ...]

the args are read and concatenated together into a single cmd, which is then read and executed by the shell; its exit status is returned as the value of the eval cmd; if there are no args, or only null args, 0 is returned;

exec [-cl][-a name][cmd [args]]

if cmd is specified, it replaces the shell; no new process is created; args become the arguments to cmd; if -l is given, shell places a dash at the beginning of 0th arg passed to cmd (like login(1)); -c causes cmd to be executed with an empty env; -a causes shell to pass name as 0th arg to the executed cmd; if cmd cannot be executed for some reason, a non-interactive shell exits, unless shell option execfail is enabled, in which case it returns failure; an interactive shell returns failure if the file cannot be executed; if cmd is not specified, any redirections take effect in the current shell, and the return status is 0; if there is 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 cmd executed;

export [-fn][name[=word]] ...

the supplied names are marked for automatic export to the env of subsequently executed cmds; with -f names refer to functions; if names are missing or -p is given, a list of all names that are exported in this shell is printed; -n option causes the export property to be removed from the named variables; return value is 0 unless an invalid option is given, one of the names is not a valid shell var name, or name supplied with –f is not a function name;

export [-p]

print all names that are exported in this shell;

fg [jobspec]

moves the specified job to the foreground, and makes it the current job; if jobspec is not present, the shell's notion of the current job is used;

help [-s][pattern]

display info about built-in cmds; if pattern is specified, help gives detailed help on all cmds matching pattern; otherwise a list of built-ins is printed; -s - short usage synonpsis;

history [n][options]

without options displays the cmd history list with line numbers; n causes to show only last n lines; options are:

-c clear the history list;
-d delete the history entry at position n;

Other options are used mainly for operations with non-default history file (default is defined by HISTFILE env variable);

jobs [-lnprs][jobspec ...]

jobs -x cmd [args ...]

the first form lists active jobs; -l lists PIDs in addition to the normal info, -p lists only PID of the job's process group leader, -n displays only jobs that have changed status since the user was last notified of their status, -r restricts output to running jobs, -s .. to stopped jobs; if jobspec is given, output is restricted to info about that job; if -x is supplied, jobs replaces any jobspec found in cmd or args with the corresponding process group ID, and executes cmd passing it args, returning its exit status;

kill [-s sigspec | -sigspec][pid | jobspec] ...

kill -l [signum]

send the signal named by sigspec to the processes named by pid or jobspec. sigspec is either a signal name such as SIGKILL or a number; signal names are case insensitive and may be given with or without SIG prefix; if sigspec is missing, then SIGTERM is assumed; -l lists the signal names; if any args are supplied with -l, the names of the specified signals are listed, and the return status is 0; kill returns true if at least one signal was successfully sent, or false if an error occurs or an illegal option is encountered;

let arg [arg ...]

each arg is an arithmetic expression to be evaluated; if last arg evaluates to 0, let returns 1, otherwise let returns 0;

local

creates a local variable (must be used within function);

logout

exit a login shell;

popd [-n][+m | -m]

removes entries from the dir stack; without args removes top dir from stack and cd to the new top dir; -n supresses cd when removing dirs from stack; +m, -m removes m-th dir from left / right of the list returned by dirs (count starts from 0);

printf format args

write args to stdout formatted as specified by format; format string takes 3 types of objects: plain char (copied to stdout as is), char escape sequence (converted and copied to stdout), format spec (each applied to a corresponding arg); in addition to std C printf formats, %b expands backslash esc seq in corresponding arg (except that \c terminates output, '\' in \’, \", \? are not removed, and octal esc starting with \0 may contain up to 4 digits) and %q outputs arg in a format that can be reused as shell input; the return is zero on success, non-zero on failure;

pushd dir

adds a dir to the top of the dir stack;

pushd [-n][+m | -m]

rotate dir stack making new top of the stack curr work dir; without args exchanges the top two dirs and returns 0 unless the stack is empty; -n cancels cd when adding dir; +m | -m rotates the stack so that m-th dir from left / right becomes the top;

pwd [-LP]

print the absolute pathname of the current working dir; with -L pathname can contain symlinks, if -P is specified pathname does not contain symlinks;

read [options][name ...]

one line is read from the stdin, and the first word is assigned to the first name, 2nd word to the second name, and so on, with left-over words assigned to the last name; if there are fewer words then names, the remaining names are assigned empty values; only chars in IFS are recognized as word delimiters; if names are not supplied, the line read is assigned to var REPLY; options:

-a name

words are assigned to sequential indices of the array variable name, starting at 0; other name args are ignored;

-d delim

the first char of delim is used to terminate the input line, rather than newline;

-e if stdin is coming from terminal, use readline;

-n m

return after reading m chars;

-p prompt

display prompt on stderr without a trailing newline before attempting to read input; the prompt is displayed only if input is coming from terminal;

-r consider backslash to be part of the line, do not interpret it as an escape char or line continuation;
-s silent mode, chars are not echoed (assuming input comes from a terminal);

-t m

time out and return failure if a complete line of input is not read within m sec; works only when input comes from terminal or pipe;

-u m

read input from file descriptor m;

The return value is 0 unless timeout, bad file descriptor (with –u option) or EOF is encountered;

readonly [-apf][name[=word] ...]

make given names read-only (possibly combining it with assignment); -a restricts scope to arrays, -f widens to functions, -p causes the list of all readonly names to be printed; the return status is 0 unless an invalid option, or one of the names is not a valid shell variable, -f is used with non-function name;

return [n]

exit [function] with the return value of n (if not specified, n = exit status of the last cmd in the func body); if used outside function, the return status is false, except when script was started by . (source) cmd: in this case it causes shell to stop exec and return n (or exit status of the last cmd executed within the script) as the exit status of the script;

set [-abefhkmnptuvxBCHP] [-o option][arg ...]

without options displays name=value of each shell variable in a format that can be reused for input; options set or unset shell attribs, any args remaining after options are treated as values for positional params and are assigned to $1, $2, etc;

shift [n]

the positional params from n+1 ... are renamed to $1 ...; params represented by numbers $# down to $#-n+1 are unset; if n is 0, no params 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 $#; the return status is greater than 0 if n is greater than $# or less than 0, otherwise 0;

shopt [-pqsu][-o][optname ...]

toggle values of variables controlling shell behaviour; without options a list of all settable options is displayed; -p does the same, but output can be reused for input; other options are:

-s set each optname;
-u unset each optname;
-q quiet mode (suppress normal output);
-o restrict the values of optname to those of set;

suspend [-f]

suspend execution of this shell until it receives SIGCONT; -f says not to complain if this is a login shell; test expr returns 0 or 1 depending on the evaluation of the conditional exppression;

times

print the accumulated user and system times for the shell and for processes run from this shell; the return status is 0;

trap [-lp][[arg] sigspec ...]

cmd arg is to be read and executed when shell receives the specified signal; sigspec is signal number or name (see <signal.h>); signal names are case insensitive, SIG prefix is optional; if -p and sigspec are present, but arg is missing, then trap cmds associated with each sigspec are printed; if nothing or -p only, the list of cmds associated with each signal is printed; -l causes the list of signal names and nums to be output; if sigspec is 0 (EXIT) arg is executed on exit from the shell;

ulimit [options [limit]]

set limit on resource available to the shell and processes started by the shell; -H, -S set hard / soft limits (hard limit cannot be increased, soft limit can be increased up to the hard limit); if neither is specified both are set; limit is either num or special value like hard, soft (current hard / soft limit) and unlimited; if limit is omitted, the current value of the soft limit (or hard, if -H) for this resource is printed; options are:

-a show all current limits;
-c max size of core files created;
-d max size of a process's data segment;
-f max size of files created by shell (default);
-l max size that may be locked into memory;
-m max resident set size;
-n max number of open file descriptors;
-p pipe size in 512-byte blocks (may not be set);
-s max stack size;
-t max amount of cpu time in seconds;
-u max num of processes available to a single user;
-v max amount of virtual mem available to the shell;

The return value is 0 unless an invalid option / arg was given or error occurred while setting new limit;

umask [-p][-S][mode]

set user file-creation mask; mode is octal if begins with a digit, otherwise a symbolic mode mask (see chmod); without mode current value of the mask is printed in octal or in symbolic form (-S) or in form that may be reused as input (-p); ret value is 0 if mask was changed or not specified, otherwise ret is false;

unalias [-a][name ...]

remove the specified aliases; with -a removes all alias definitions; return value is true unless wrong name is given;

unset [-fv][name ...]

remove specified variables (-v, default) or functions (-f); read-only variables may not be unset; exit status is true unless variable is read-only;

wait [n]

wait for the specified process and return its termination status; n is pid or job spec; in latter case all processes in that job’s pipeline are waited for; without n all currently active child proc are waited for and return status is 0; if non-existent proc / job is specified, return status is 127; otherwise, the return status is the exit status of the last process / job waited for;