Built-in Shell commands

: (a colon)

: [arguments]

 

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

. (a period)

. filename [arguments]

 

Read and execute commands from the filename.

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

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

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. It directory is not given, then the value o 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.

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.

unset

unset [options] {name}

 

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


2020–07–19