In this guide, we will learn more about the "type" command in Linux.
To perform the steps demonstrated in this guide, you will need the following components:
Unlike other Linux-specific commands (such as: ls, chmod, shut, vi, grep, pwd, etc.), the "type" command is a built-in Bash function that displays information about the command type provided as a parameter.
$type
In addition to Bash, other shells (Zsh, Ksh, etc.) also come with "type" commands that they execute.
Basic usage
The command structure of typeis as follows:
$type
The simplest usage is to provide the command as a parameter.
$type, where
The "type" command can also handle multiple parameters at the same time.
$type type which cd ls chmod sudo sleep shoff
Command type
If we are only interested in the type of command, then use the "-t" flag to get only the command type.
$type—t echo
$type—t type
$type—t while
$type—t grep
The output is one of the following command types:
All locations
Various commands on Linux exist both as stand-alone executable files and as built-in shell functions. We can use the "type" command to determine whether a command has these two characteristics.
Check out the examples below:
$TYPE-AEcho
Here, as the output shows, the "echo" command is a built-in shell function and an executable file (located in "/usr/bin/echo").
In addition to the options mentioned above, the "type" command also comes with some additional options.
"—p" flag
Run the following command:
$type—p echo
$type-pClose
Here, if the given parameter is a built-in shell, the "type" command will not display any output. Otherwise, the output will be the location of the command's executable file.
"P" flag
Check out the examples below:
$P type echo power supply
$type—P,while
Here, the "type" command will search all PATH locations for the given parameters and return the location of the matching executable file.
In this example, the "echo" and "pwd" commands have dedicated binaries. "if" and "while" are both shell keywords and have no dedicated binary code. The output is empty.
After performing its task, the "type" command leaves an exit code. Using the exit code, we can determine whether the task was successful.
The following command demonstrates the exit code:
$type
$echo $?
$type asdfg
$echo $?
In this guide, we demonstrate how to use the "type" command in Linux. It is a shell built-in command that describes the nature of the command.
Interested in learning more about other Linux commands? Check out the Linux commands subcategory. For more Bash-related guides, check out Programming with Bash.
Happy computing!
The above is the detailed content of Linux type command. For more information, please follow other related articles on the PHP Chinese website!