In this guide, we will learn more about the “type” command in Linux.
Prerequisites:
To perform the steps that are demonstrated in this guide, you need the following components:
- A properly configured Linux system. Check out how to create a Linux VM for testing and learning purposes.
- Basic understanding of the command-line interface
The Type Command in Linux
Unlike other Linux-specific commands (for example: ls, chmod, shutdown, vi, grep, pwd, etc.), the “type” command is a built-in Bash function that displays an info on the type of the command that is provided as an argument.
Besides Bash, other shells (Zsh, Ksh, etc.) also come with their own implementation of the “type” command.
Basic Usage
The command structure of type is as follows:
The simplest usage is to provide a command as an argument.
The “type” command can also work with multiple arguments at once.
Command Types
If we are interested in only the type of the command, use the “-t” flag to get the command type only.
The output is one of the following command types:
- alias: A shell alias.
- function: A built-in shell function.
- builtin: A built-in shell command.
- file: A disk file.
- keyword: A reserved word for shell-specific functions.
Displaying All Locations
Various commands on Linux exist as both a standalone executable file and a built-in shell function. We can determine if a command has both characteristics using the “type” command.
Check out the following example:
Here, as the output suggests, the “echo” command is a built-in shell function as well as an executable file (located at “/usr/bin/echo”).
Other Options
Besides the options that are mentioned so far, the “type” command comes with some additional ones.
The “-p” Flag
Run the following commands:
Here, the “type” command won’t show any output if the given argument is a shell built-in. Otherwise, the output will be the location of the command’s executable file.
The “-P” Flag
Check out the following examples:
Here, the “type” command will search for the given arguments in all the PATH locations and return the location of the matching executable files.
In this example, both “echo” and “pwd” commands have dedicated binaries. Both “if” and “while” are shell keywords and have no dedicated binary. So, the output is empty.
Exit Codes
After performing its task, the “type” command leaves behind an exit code. Using the exit code, we can determine if the task is successful or not.
- 0: The command runs successfully and without any error.
- 1: The command faces an error.
The following commands demonstrate the exit codes:
$ echo $?
$ echo $?
Conclusion
In this guide, we demonstrated how to use the “type” command in Linux. It’s a shell built-in command that describes the nature of a command.
Interested in learning more about other Linux commands? Check out the Linux commands sub-category. For more Bash-related guides, check out about Bash programming instead.
Happy computing!