Technical skill

Bash syntax: Builtin Commands

A shell can change its own folder

Bash is a program, called a shell, that reads commands typed at a prompt. The cd command tells Bash to use a different folder, or directory, so the next command runs there. Bash handles cd itself; commands it runs inside the shell are called builtins. A separate program runs as an independent process, a running copy of a program. When it ends, Bash is still in its original folder, so that program cannot change the directory of the shell that started it.

Other commands are separate programs stored in files. Bash finds them by searching folders listed in PATH, a list of places to look for commands. A command name can also be an alias, a short name Bash expands, or a function, a named group of shell commands. Use type to see what a name refers to and help to read about builtins:

type -a cd printf grep
help read
help printf

Aliases are expanded while Bash reads a command. It then checks functions and builtins before searching PATH for an executable. The command builtin can skip a shell function with the same name when running a command. enable shows which builtins are active or changes their availability.

Commands such as cd, export, umask, read, trap, set, and unset need access to Bash's own state, which is why they are builtins. Bash also provides mapfile, shopt, and directory-stack commands. POSIX, a shared standard for shell behavior, requires a smaller set. A script that uses Bash-only features should name Bash in its interpreter line.

Read a file without losing shell variables

The read builtin takes one line of input and assigns its fields to variables. This loop prints each line from a file:

while IFS= read -r line; do
    printf '%s\n' "$line"
done < "$file"

IFS= keeps read from trimming leading or trailing whitespace; -r leaves backslashes as ordinary characters. Redirecting the file into the loop keeps it in the current shell, so assignments remain available afterward. If a pipeline feeds the loop, common Bash configurations run it in a subshell, a temporary shell started to do work. Its variable changes disappear when it ends.

For formatted output, printf takes a quoted format string and separate values. The behavior of echo differs between implementations when options or backslashes are involved. Keep untrusted input out of the format string.

Know what ends and what continues

break leaves a loop; continue skips to its next iteration. return leaves a function or a script loaded into the current shell with .. exit ends the shell process. A wrapper that is finished and should hand control to another program can use exec to replace the current shell with that program.

Use help name for a quick description of a builtin. POSIX also defines special builtins with particular lookup, error, and assignment rules. Bash documents those rules and how they apply in POSIX mode.

Interactive exercises

Materials

Skillsets
Skillset Devops preview
image descriptionDevops
by Dio
3 users
Skillset Linux administrator preview
image descriptionLinux administrator
by Infinite Education
3080 users
Skillset Devops preview
image descriptionDevops
by Ajay Kumar
2004 users
Do you know that ...?
Any useful or interesting facts about the skill. Any peace of information would be useful