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 is its executable 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:
christopher@linux-handbook:~$ 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:
christopher@linux-handbook:~$ type mkdir mkdir is /usr/bin/mkdir
In the above case, it locates the executable of the mkdir command. This is similar to the which command but type is faster because it is a built-in shell command.
If you use it with something that is not a command, it gives a not found error.
christopher@linux-handbook:~$ type no_command bash: type: no_command: not found
2. 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:
christopher@linux-handbook:~$ type ll ll is aliased to `ls -alF'
As you can see, it shows the real command behind the aliased one.
3. Get the type of multiple commands
You can also use type with multiple commands and get the results echoed back to us.
christopher@linux-handbook:~$ 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.
4. 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.
christopher@linux-handbook:~$ type -P ls /usr/bin/ls
This will return the path name even if it is an alias, built-in, or function.
5. Get all information of the commands
We can get the most complete information using option -a.
christopher@linux-handbook:~$ 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.
6. 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:
christopher@linux-handbook:~$ type -t ls alias christopher@linux-handbook:~$ type -t echo builtin christopher@linux-handbook:~$ type -t sort file christopher@linuxhandbook:~$ type -t _mac_addresses function christopher@linuxhandbook:~$ type -t if keyword
Bonus: Why do you see “command is hashed”?
Some times when you’ll see an output like command is hashed along with the path to the executable:
christopher@linux-handbook:~$ 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 program 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.
Christopher works as a Software Developer in Orlando, FL. He loves open source, Taco Bell, and a Chi-weenie named Max. Visit his website for more information or connect with him on social media.