آموزش دستور type در shell
On the Linux command line, you’ll come across several types of utilities. Some are an alias, while others are built-in tools and even functions. So, how do you check these types? Well, there exists a command ‘type‘ that offers you this information. In this tutorial, we will discuss the ‘type’ command using some easy to understand examples.
But before we do that, it’s worth mentioning that all examples here have been tested on an Ubuntu 18.04 LTS machine.
Linux type command
As already mentioned in the introduction above, the type command in Linux displays information about command type. Following is its syntax:
type [-afptP] name [name ...]
And here are some Q&A-styled examples that should give you a better idea on how the type command works.
Q1. How to use type command?
It’s simple, just execute ‘type’ with the command line tool name as input and -t as the command line option. For example,
type -t cp
This command produced the following output:
file
Similarly, if the tool name is an alias, then the type command clearly tells that. For example, the following command:
type -t ls
produced this output:
alias
Here’s how the tool’s man page explains the -t option:
-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
For this, execute the type command sans any option. Here’s an example:
Use the -a command line option for this. Here’s an example:
type -a ls
This command produced the following output on my system:
ls is aliased to `ls --color=auto'
ls is /bin/ls
Q4. How to make type search even if input is an alias?
By default, if you input an alias (like ‘ls’ on some systems), the type command doesn’t produce location in output. For example:
type ls
Here’s the output:
ls is aliased to `ls --color=auto'
However, if you want, you can force ‘type’ to search for location even if the input is an alias. This can be done using the -P command line option.
-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
Conclusion
The type command doesn’t have a steep learning curve. Many of the options it provides we’ve discussed in this tutorial. Once you’re done testing these, head to the ‘type’ command man page to learn more about it.