The type command is a built-in bash shell command that can provide the type of a specified command.
What does it mean by the “type of a command”? It means that you can get information like whether a Linux command is built-in shell command, where its executable is located and whether it is aliased to some other command.
It may seem like it’s not of much use but believe me it could come handy while investigating why a command is behaving in a certain way.
Using type command in Linux
The syntax for the type command is simple:
type [options] name
To get started, let’s use the type command without options on the well-known echo command:
[email protected]:~$ type echo echo is a shell builtin
It tells us that echo is a shell built-in command. This is the type of command that would run if the name echo is interpreted by the command line.
Let’s try something else:
[email protected]:~$ type mkdir mkdir is /usr/bin/mkdir
If you use it with something that is not a command, it gives a not found error.
[email protected]:~$ type no_command bash: type: no_command: not found
Type of an aliased command
You’re probably already familiar with aliases in Linux. To quickly recall, these are pseudo commands that work like shortcuts. They can be set in your shell profile.
Let’s see what kind of information type command finds when you use it on an aliased command:
[email protected]:~$ type ll ll is aliased to `ls -alF'
As you can see, it shows the real command behind the aliased one.
Get the type of multiple commands
You can also use type with multiple commands and get the results echoed back to us.
[email protected]:~$ type ls ll ls is aliased to `ls --color=auto' ll is aliased to `ls -alF'
On Ubuntu and some other distributions, ls is aliased to show you a colorful output. This helps you in distinguishing the symlinks, hard links, directories, executable and other different type of files.
Force type to return the path of the commands
If you want to locate the executable of a command and type keeps giving output like built-in shell and alias information, you can force to get the path with -P option.
[email protected]:~$ type -P ls /usr/bin/ls
This will return the path name even if it is an alias, built-in, or function.
Get all information of the commands
We can get the most complete information using option -a.
[email protected]:~$ type -a ls ls is aliased to `ls --color=auto' ls is /usr/bin/ls ls is /bin/ls
This shows us both the type information and every location on the system path with the file.
Return only the type of command, not path
Here’s different type you can get:
You can prompt for only the type with the -t option. Here are a few examples:
[email protected]:~$ type -t ls alias [email protected]:~$ type -t echo builtin [email protected]:~$ type -t sort file [email protected]:~$ type -t _mac_addresses function [email protected]:~$ type -t if keyword
Bonus: Why do you see “command is hashed”?
Sometimes you see an output like "command is hashed" along with the path to the executable:
[email protected]:~$ type man man is hashed (/usr/bin/man)
To avoid spending too much time on searching the path of an executable, the shell often keeps a list of programs it has found in the past. This list is called ‘hash’.
When you see an output liked ‘command is hashed’, it means that the type command is returning the result from the already performed searches. You can use hash -r to force the shell to search from scratch.
I hope you learned something new today with this introduction of the type command in Linux. I find it similar to the file command which is used for getting information about files.
If you like this guide, please share it on social media. If you have any comments or questions, leave them below. If you have any suggestions for topics you’d like to see covered, feel free to leave those as well. Thanks for reading.