- Beginners Guide for Type Command in Linux
- Tutorial Details
- Syntax of the Type Command
- Identifying the Actual Type of the Referenced Command
- Trim the Type Command Output
- Force Type to Return the Path of the Referenced Command
- Getting More Information About the Referenced Command
- What Does “Command is Hashed” Mean in the Output?
- Conclusion
- Type Command in Linux – (How to) Display Information About Command
- Get Information About a Command With Type Command in Linux
- Using type command in Linux
- Type of an aliased command
- Get the type of multiple commands
- Force type to return the path of the commands
- Get all information of the commands
- Return only the type of command, not path
- Bonus: Why do you see “command is hashed”?
- Conclusion
Beginners Guide for Type Command in Linux
The type command tells you the actual type of the referenced command. With this information, you can figure out how a command will be interpreted when you execute it in the terminal.
The following is the list of known command types:
Aside from showing the file type, this command can also be used to find and show all the locations that contain the referenced command (in binary).
Knowing all the things it offers might discourage your desire to go forward, but trust me, this command will come in handy, especially when you’re trying to figure out why a command is acting in a certain way.
So, read this article to find out everything you need to know about this command, including its different options (with practical examples).
Tutorial Details
Description | Type |
Difficulty Level | Low |
Root or Sudo Privileges | No |
Host System and Architecture | Ubuntu 22.10 (x64) |
OS Compatibility | Ubuntu, Manjaro, Fedora, etc. |
Prerequisites | type |
Internet Required | No |
Discussed Tools in this Article |
Syntax of the Type Command
The type command takes two arguments: one is the option, and the other is the command or file name.
$ type [OPTION] [COMMAND or FILE NAME]
Identifying the Actual Type of the Referenced Command
To make things easier for beginners, let’s pass the most commonly used echo command as an argument to the type command without any options.
As you can clearly see, the echo command is a shell built-in command and is interpreted in this way whenever it is executed.
Note that you are not limited to passing a single command; you can pass multiple commands to the type command at the same time to find their actual types.
$ type ls cp if
In the above picture, it states that “ ls ” is an alias for the “ ls —color=auto ” command. Check out our article on the ls command and creating shortcuts in Linux to know why.
The cp command is referring to the “/usr/bin/cp” binary, and “ if ” is a shell keyword (mostly used in shell scripting).
Trim the Type Command Output
Instead of getting the extra information in the output, you can trim the results into just the type of the command using the “ -t ” flag.
The following is the list of known command types:
The following is an example of when a single command is passed to a type command with the “ -t ” flag.
$ type -t echo
The following is an example when multiple commands are passed to type command with “ -t ” flag.
$ type -t ls cp if
Force Type to Return the Path of the Referenced Command
If you are only interested in finding the executable path of the referenced command without worrying whether its built-in shells or aliases, then you can use the “ -P ” flag.
$ type -P echo
When you pass the mixtures of multiple commands to find their executable path you will get the following results.
$ type -P ls cp if
As you can clearly see from the above picture, the executable paths for the ls and cp commands return in output, except for “ if “, which is a shell keyword and not an executable file.
Getting More Information About the Referenced Command
The “ -a ” flag is useful when you want to know the type of the command and all the places where the referenced command (executable file) can be found.
In the following example, the echo command is being passed with the “ -a ” flag.
You will get a combination of mixed results when you pass the multiple commands with the “ -a ” flag.
$ type -a ls cp if
What Does “Command is Hashed” Mean in the Output?
Sometimes, when you check the type of the referenced command, you might end up getting “command is hashed” along with the path in the output.
The reason for this output, even though it may only occasionally occur, is that your shell keeps a record of every executable program that it has ever discovered in a list known as the hash list.
This way, the shell avoids wasting time searching for the path of an executable by returning the results from previous searches, even though you can use the “ hash -r ” command to force the shell to start searching from scratch.
Conclusion
If you read the complete article, then you realize how useful and handy this tool can be when you want to determine the command type or how it’s acting in a certain way.
If you have questions or queries related to this topic, then feel free to ask them in the comment section.
Type Command in Linux – (How to) Display Information About Command
TYPE is a Linux command which helps to identify the type of the input command if it is an alias, built-in, function, or keyword. You can also pass multiple commands as the input parameters.
All the arguments to this command are optional.
- -a – display all locations containing an executable named NAME; includes aliases, builtins, and functions, if and only if the ‘-p’ option is not also used
- -f – suppress shell function lookup
- -P – force a PATH search for each NAME, even if it is an alias, builtin, or function, and returns the name of the disk file that would be executed
- -p – returns either the name of the disk file that would be executed or nothing if `type -t NAME’ would not return `file’.
- -t – output a single word which is one of `alias’, `keyword’, `function’, `builtin’, `file’ or `’, if NAME is an alias, shell reserved word, shell function, shell builtin, disk file, or not found, respectively
Name – Command name to be interpreted.
Exit Status – Returns success if all of the NAMEs are found; fails if any are not found.
In Linux, Unix, and Unix-alike system command may an alias, shell built-in, file, function, or keyword. So how to find the type of command you are running on the shell.
Get Information About a Command With Type Command in Linux
The type command tells you whether a Linux command is built-in shell command, where is its executable located and whether it is aliased to some other command. Here’s how to use the type command in Linux.
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:
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.
[email protected]:~$ 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.
[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.
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.
Conclusion
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.