- Unix Commands Reference
- Unix Commands - Home
builtin Command in Linux
The builtin command is useful tool that allows you to define a shell function with the same name as a shell builtin, retaining the functionality of the builtin within the function. Built-in commands are those commands that are integrated into the shell itself, such as cd, echo, cat, ls and more.
You can pass arguments to the built-in command using builtin. For example, if you want to change the directory using the built-in cd command, you can pass the target directory as an argument. The builtin command returns the exit status of the built-in command it runs.
The exit status is a number that indicates whether the command was successful (usually 0) or if there was an error (non-zero). You can also create a shell function with the same name as a built-in command. For instance, you can have a custom cd function that does additional tasks when changing directories.
With builtin, you can ensure that within your custom shell function, the original built-in command is still executed. This way, you can extend the functionality of the built-in command without losing its original behavior.
Table of Contents
Here is a comprehensive guide to the options available with the builtin command −
Syntax of builtin Command
The following is the general syntax for the builtin command −
builtin [shell-builtin [arg ..]]
Where −
- builtin − This is the command itself.
- shell-builtin − This is the name of the built-in command you want to execute.
- arg − These are the arguments you want to pass to the built-in command.
Examples of builtin Command in Linux
In this section, we’ll explore basic examples of the builtin command using different shell built-in commands and arguments. Ensure you have sudo or root privileges.
- Using cd to Change Directory
- Using echo to Print a Message
- Using exit to Terminate the Shell with a Status Code
- Using export to Set Environment Variables
- Using alias to Create Shortcut for Commands
- Using pwd to Print the Current Working Directory
- Using set to Set Shell Options
- Using ulimit to Set User Limits
- Using type to Display Information about a Command
- Using buitin in Bash Script
Using cd to Change Directory
To change the present working directory to another directory, you can simply use the following command −
builtin cd /home/data
This command will change the current directory to /home/data using the built-in cd command.
Using echo to Print a Message
To print a message using the builtin command, you can simply use the following syntax −
builtin echo "Hello, World!"
By using builtin echo "Hello, World!", you’re ensuring that the original built-in echo command is used to print the message, bypassing any functions or aliases named echo that might exist.
Using exit to Terminate the Shell with a Status Code
To terminate a shell session or script and return an exit status code to the calling process, you can use the following command −
builtin exit 1
The builtin command ensures that the shell’s built-in version of exit is used, rather than any external command named exit. The 1 specifies the exit status code. In this case, the shell will exit with a status code of 1, which typically indicates a general error or failure.
Using export to Set Environment Variables
To set an environment variable and make it available to child processes, you can use the following syntax −
builtin export PATH=/usr/local/bin:$PATH
This command updates the PATH environment variable to include /usr/local/bin at the beginning, ensuring that any executables in /usr/local/bin are found before those in other directories listed in PATH.
Using alias to Create Shortcut for Commands
To create a shortcut, or alias, for a longer command in Unix/Linux systems, you can use the following syntax −
builtin alias ll='ls -la'
After running this command, you can simply type ll in the terminal instead of ls -la, and it will execute the same command. This can save time and make your command-line usage more efficient.
To make this alias available every time you launch your terminal, you can add it to your shell’s configuration file (e.g., .bashrc or .zshrc) −
echo "alias ll='ls -la'" >> ~/.bashrc source ~/.bashrc
This way, the alias will be set up permanently whenever you start a new terminal session.
Using pwd to Print the Current Working Directory
To display the absolute path of your current working directory, you can simply run the following command −
builtin pwd
In this example, the command outputs /home/Tutorialspoint, indicating that the current working directory is the user’s home directory.
Using set to Set Shell Options
To set a shell option that prevents overwriting existing files in Unix/Linux systems, you can use the following syntax −
builtin set -o noclobber echo "my_text" > existing_file.txt
In this example, the shell prevents overwriting existing_file.txt because the noclobber option is set.
Using ulimit to Set User Limits
To set the maximum number of open file descriptors for the current shell sessions, you can use the following command −
builtin ulimit -n 1024
This command sets the limit on the number of open file descriptors to 1024 for the current shell session. This is useful for controlling the number of files a process can open simultaneously.
Using type to Display Information about a Command
To display information about a command, you can use the following syntax −
builtin type cd
Using buitin in Bash Script
The following bash script shows you how you can use the builtin command in a bash script to ensure that the original built-in command is executed within a custom function.
To get started, create a script file using your preferred text editor −
nano custom_builtins.sh
Add the following lines to the code −
#!/bin/bash #builtinsample # Change directory and print a message cd() { echo "Changing directory to $1" builtin cd "$1" } # Print the current working directory pwd() { echo "Current directory is:" builtin pwd } # Using the custom cd function cd /home/user # Set an environment variable and print a message set_env() { echo "Setting environment variable MY_VAR to $1" builtin export MY_VAR="$1" } # Using the custom set_env function set_env "HelloWorld" # Print the environment variable echo "MY_VAR is set to: $MY_VAR"
Save the script to a file and make it executable with the following command −
chmod +x custom_builtins.sh
Then run the script −
./custom_builtins.sh
Conclusion
The examples above demonstrate how you can use the builtin command to ensure that the original built-in commands are executed, even if you have custom functions or aliases with the same names. You can now explore this command-line utility to gain a deeper understanding of it.