Built-in Shell commands

. (a period)

. filename [arguments]

 

Read and execute commands from the filename.

This is useful to read a configuration file, to initialise the script’s parameters and to import functions:

SCRIPT_DIR=$(dirname "$0")
. "${SCRIPT_DIR}/tools" || { echo "Error: '${SCRIPT_DIR}/tools' is missing."; exit 1; }

: (a colon)

: [arguments]

 

Expand arguments and perform redirections; otherwise do nothing. The return status is 0.

break

break [n]

 

Exit from a for, select, until or while loop.

If a positive integer n is supplied, then the execution of the nth enclosing loop is exited.

The return status is 0 unless n is not valid.

cd

cd [options] [directory]

 

Change the current working directory to directory. If directory is not given, then the value of the HOME shell variable is used.

The return status is 0 if the directory is successfully changed; non-zero otherwise.

continue

continue [n]

 

Resume the next iteration of an enclosing for, select, until or while loop.

If a positive integer n is supplied, then the execution of the nth enclosing loop is resumed.

The return status is 0 unless n is not valid.

exit

exit [n]

 

Exit the shell, returning a status of n to the shell’s patent.

If n is omitted, then the exit status of the last command executed is used.

Any trap on EXIT is executed before the shell terminates.

getopts

getopts string name {argument}

 

Parse positional parameters sent to the shell.

string contains the option characters to be recognised. If a character is followed by a colon, then the option is expected to have an argument, which should be separated from it by space. The colon and question mark cannot be used as option character.

Each time it is invoked, getopts places the next option in the shell variable name and the index of the the next argument to be processed into the variable OPTIND. When an oprion requires an argument, getopts places that argument into the variable OPTARG.

getopts normally parses the positional parameters, but if one or more argument are given, then it parses those instead.

let

let argument {argument}

 

Evaluate each argument as a mathematical expression. The arguments are evaluated from left to right.

In Bash, let is similar to enclosing an arithmetic expression in double parentheses:

(( expression ))

pwd

pwd [options]

 

Print the absolute pathname of the current working directory.

return

return [n]

 

Cause a shell function to exit with the return value n.

If n is not supplied, then the return value is the exit status of the last command executed in the function.

shift

shift [n]

 

Shift the positional parameters to the left by n.

If n is not supplied, then it is assumed to be 1.

The return status is 0, if 0 < n$#, and non-zero otherwise. See also: Built-in Shell Variables.

unset

unset [options] {name}

 

Each variable or function name is removed from the memory and the shell’s exported environment.

wait

wait [ID]

 

Wait for a process or job to complete.

With no ID, pause in execution until all background jobs complete and exit with status 0; otherwise pause until the specified background process ID or job ID completes and the exit status of ID is returned.

Example:

# wait until the most recent background process is finished

wait $!

2022-01-15