How to use the command 'builtin' (with examples)
The builtin
command in Unix-like operating systems is a shell utility designed to execute shell built-in commands. Built-in commands are shell functions that are integral to the shell itself, as opposed to external utility programs. The primary purpose of the builtin
command is to ensure that the shell runs a built-in command rather than any external program that might have the same name, thus maintaining the expected behavior and offering better performance since there is no need to create a new process.
Use case 1: Run a shell builtin
Code:
builtin echo "Hello, World!"
Motivation:
In a Unix-like system, certain common commands exist as both shell built-ins and external utilities. For instance, the echo
command is both a built-in in most shells and available as an external utility. In scripts and commands, you might want to ensure that you’re running the built-in version to maintain the script’s speed and memory efficiency. Using builtin
, you avoid the system call overhead associated with executing an external program.
Explanation:
builtin
: This keyword ensures that the command following it is executed as a built-in function of the shell, bypassing any external versions of the command.echo
: This is the command being executed, used to display a line of text/string. Here, it is specifically the shell’s built-inecho
."Hello, World!"
: This is the argument being passed to theecho
command, representing the text that will be output to the terminal.
Example Output:
Hello, World!
The output will simply display the string “Hello, World!” on the terminal, confirming the execution of the built-in echo
command.
Conclusion:
The builtin
command serves as a valuable tool for ensuring that scripts and command-line operations utilize the shell’s built-in functions to their fullest potential. By executing built-in commands directly, users can benefit from more efficient processing, as the execution does not require the additional overhead of starting a new process for an external utility. This feature becomes especially beneficial in environments where performance and resource allocation are critical factors.