Linux type command
type is a built-in shell command in Linux/Unix systems used to display type information about a specified command. It can tell you whether a command is:
- a built-in shell command
- an external executable program
- a shell function
- Alias
This command is very helpful for understanding how the system parses and executes commands, especially when encountering command conflicts or wanting to know the source of a command.
Basic syntax of the type command
type [选项] 命令名 [命令名...]
Description of common options
| Option | Description |
|---|---|
-t |
Show only the command type (short output) |
-p |
If it is an external command, display its path |
-P |
Force a lookup in PATH even if the command is built in |
-a |
Show all possible definitions (including aliases, builtins, external, etc.) |
Output types of the type command
When using type command, you may see the following output types:
Alias (alias)
$ type ll
ll is aliased to `ls -alF'
Builtin command (builtin)
$ type cd
cd is a shell builtin
External command (file)
$ type python
python is /usr/bin/python
shell function (function)
$ type myfunc
myfunc is a function
myfunc ()
{
echo "This is a function"
}
Keyword (keyword)
$ type if
if is a shell keyword
Practical Application Examples
Example 1: Check command type
$ type ls
ls is aliased to `ls --color=auto'
Example 2: View all possible definitions
$ type -a echo
echo is a shell builtin
echo is /bin/echo
Example 3: Show only the command type
$ type -t cd
builtin
Example 4: Find the path of an external command
$ type -p git
/usr/bin/git
Practical scenarios for the type command
-
Debugging command conflicts
When multiple commands with the same name exist (such as a builtin and an external command),
type can help you determine which one is actually executed.
2.
Understanding command sources
When writing scripts or learning the system, knowing whether a command is builtin or external helps you understand its behavior and performance characteristics.
Verifying whether a command exists
can quickly check whether a command is available and where it is located.
Learning the shell environment
By viewing the types of various commands, you can better understand how the shell works.
Notes
typeis a shell builtin command; implementations may differ slightly across shells (bash, zsh, ksh, etc.)- Use
-aoption, the output order usually reflects the command lookup order - Some shells may not support all options; you can consult
help typefor specific help
Through these exercises, you will gain a better grasp of type the practical applications of the command.
Linux Command Reference