- Linux Type Command
- How to Use the type Command #
- Command Types #
- Display all locations that contain the command #
- Other type command options #
- Conclusion #
- Команда type в Linux
- Как использовать команду типа
- Типы команд
- Показать все местоположения, содержащие команду
- Опции команд другого типа
- Выводы
- 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
- Type Command in Linux – (How to) Display Information About Command
Linux Type Command
The type command is used to display information about the command type. It will show you how a given command would be interpreted if typed on the command line.
In this article, we will explain how to use the Linux type command.
How to Use the type Command #
type is a shell builtin in Bash and other shells like Zsh and Ksh. Its behavior may be slightly different from shell to shell. We will cover the Bash builtin version of type .
The syntax for the type command is as follows:
For example, to find the type of the wc command , you would type the following:
The output will be something like this:
You can also provide more than one arguments to the type command:
The output will include information about both sleep and head commands:
sleep is /bin/sleep head is /usr/bin/head
Command Types #
The option -t tells type to print a single word describing the type of the command which can be one of the following:
- alias (shell alias)
- function (shell function)
- builtin (shell builtin)
- file (disk file)
- keyword (shell reserved word)
Display all locations that contain the command #
To print all matches, use the -a option:
The output will show you that pwd is a shell builtin but it is also available as a standalone /bin/pwd executable:
pwd is a shell builtin pwd is /bin/pwd
When -a option is used, the type command will include aliases and functions, only if the -p option is not used.
Other type command options #
The -p option will force type to return the path to the command only if the command is an executable file on the disk:
For example, the following command will not display any output because the pwd command is a shell builtin.
Unlike -p , the uppercase -P option tells type to search the PATH for an executable file on the disk even if the command is not file.
When the -f option is used, type will not look up for shell functions, as with the command builtin.
Conclusion #
The type command will show you how a specific command will be interpreted if used on the command line.
If you have any questions or feedback, please leave a comment below.
Команда type в Linux
Команда type используется для отображения информации о типе команды. Он покажет вам, как данная команда будет интерпретироваться, если ввести ее в командной строке.
В этой статье мы объясним, как использовать команду type Linux.
Как использовать команду типа
type — это оболочка, встроенная в Bash и другие оболочки, такие как Zsh и Ksh. Его поведение может немного отличаться от оболочки к оболочке. Мы рассмотрим встроенную в Bash версию type .
Синтаксис команды type следующий:
Например, чтобы найти тип команды wc , вы должны ввести следующее:
Результат будет примерно таким:
Вы также можете предоставить более одного аргумента команде type :
Вывод будет включать информацию о командах sleep и head :
sleep is /bin/sleep head is /usr/bin/head
Типы команд
Параметр -t указывает type напечатать одно слово, описывающее тип команды, которое может быть одним из следующих:
- псевдоним (псевдоним оболочки)
- функция (функция оболочки)
- встроенный (встроенный в оболочку)
- файл (файл на диске)
- ключевое слово (зарезервированное слово оболочки)
Показать все местоположения, содержащие команду
Чтобы распечатать все совпадения, используйте параметр -a :
Вывод покажет вам, что pwd — это встроенная оболочка, но она также доступна как автономный исполняемый файл /bin/pwd :
pwd is a shell builtin pwd is /bin/pwd
Когда используется опция -a , команда type будет включать псевдонимы и функции, только если опция -p не используется.
Опции команд другого типа
Параметр -p заставит type вернуть путь к команде, только если команда является исполняемым файлом на диске:
Например, следующая команда не будет отображать никаких выходных данных, потому что команда pwd является встроенной оболочкой.
В отличие от -p , опция -P в верхнем регистре указывает type искать по PATH исполняемого файла на диске, даже если команда не является файлом.
Когда используется опция -f , type не будет искать функции оболочки, как со встроенной командой.
Выводы
Команда type покажет вам, как будет интерпретироваться конкретная команда, если она используется в командной строке.
Если у вас есть вопросы или отзывы, оставьте комментарий ниже.
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.
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.
Consider pwd command, is it shell built-in or function?
The shell provides a unique command type to find out this. Open the Linux terminal and run the command as shown below.
$ type -a pwd pwd is a shell builtin
The output tells us that pwd is shell built-in.
What about the ls command?
type ls ls is aliased to `ls --color=auto'
The type itself is a shell built-in. You can find this by running on itself.
$ type type type is a shell builtin
Now, here we will cover all options along with Examples,
1. -a option displays all locations containing an executable named NAME along with its type.
$ type -a pwd pwd is a shell builtin pwd is /bin/pwd
You can also pass multiple names of the command as input to this command.
$ type -a pwd wc pwd is a shell builtin pwd is /bin/pwd wc is /usr/bin/wc wc is /usr/bin/wc
2. -f option suppresses the shell function lookup.
$ type -f pwd pwd is a shell builtin
3. -p returns the name of the disk file that would be executed along with the complete path for alias, built-in, or function.
4. -t option simply returns type like builtin, function, alias, etc
So, we covered the basic working of the Linux Type command with examples. At any point of time, if you want help, just run man or help type on the terminal.