type Command in Linux



The type command in Linux displays information about the command type. It checks how the shell interprets a command name, whether it is an alias, a shell function, a shell built-in, or a binary executable. When a command is entered, the shell searches in a specific order: it first looks for an alias, then a function, then a built-in, and finally a binary file stored in the filesystem.

Table of Contents

Here is a comprehensive guide to the options available with the type command −

Syntax of type Command

The syntax of the Linux type command is as follows −

type [options] [command…]

In the above syntax, the [options] is an optional argument used to specify options that modify how the type command behaves. The [command...] argument is the name of the command (or multiple commands) that need to be checked.

Options of type Command

The options of the type command in Linux are listed below −

Option Description
-a Show all locations of a command (aliases, builtins, functions) unless -p is also used.
-f Skip shell function lookup.
-P Force a PATH search and show the disk file even if an alias, a builtin, or a function exists.
-p Return the disk file name that would be executed, or nothing if not a file.
-t Output one word: alias, keyword, function, builtin, file, or empty if not found.

Examples of type Command in Linux

This section demonstrates how to use the type command in Linux with examples −

Checking the Command Type

To check the type of a command, use the type command followed by the command name. For example, to check the type of cat, use the following command −

type cat
type Command in Linux1

Similarly, to check the command type of if, use the following command −

type if
type Command in Linux2

There are different command types. A list of command types is given below −

Command Type Meaning
alias A shortcut or custom name for another command (example: alias ll='ls -la').
keyword A shell reserved word, part of the shell syntax (example: if, for, then, while).
function A user-defined shell function (a block of shell commands grouped together).
builtin A command built directly into the shell (example; cd, echo, type itself).
file A standalone executable binary or script found in the filesystem (example: /bin/ls).
(empty) Returned when the name is not found, it does not exist in any form.

Checking the Command Type of Multiple Commands

Execute the following command to check the command types of multiple commands −

type cat echo
type Command in Linux3

Listing all Possible Locations of a Command

To list all possible locations of a command, including both alias and binary, use the -a option −

type -a ls
type Command in Linux4

Printing only the Type

To display the type of the command without the additional details, use the -t option −

type -t ls
type Command in Linux5

Similarly, types of the echo and cat commands are shown in the following image −

type Command in Linux6

Displaying the Real Executable

To force disk file lookup, ignoring the alias and built-ins, use the -P option with the type command −

type -P cat

Conclusion

The type command in Linux is useful for determining how the shell interprets a command, whether it is an alias, function, built-in, or executable file. It follows a search order, first checking for aliases, then functions, built-ins, and finally executable files. The command offers several options to customize its behavior, such as displaying all possible locations of a command, skipping function lookup, or forcing a path search for executable files.

Advertisements