LinuxCommandLibrary

type

Identify command type

TLDR

Display the type of a command

$ type [command]
copy

Display all locations containing the specified executable (works only in Bash/fish/Zsh shells)
$ type -a [command]
copy

Display the name of the disk file that would be executed (works only in Bash/fish/Zsh shells)
$ type -p [command]
copy

Display the type of a specific command, alias/keyword/function/builtin/file (works only in Bash/fish shells)
$ type -t [command]
copy

SYNOPSIS

type [-afptP] name [name ...]

PARAMETERS

-a
    Display all occurrences of name. For example, if name is both an alias and an executable in PATH, both will be shown. This is particularly useful when multiple versions of a command exist.

-f
    Suppress shell function lookup. This option is primarily for compatibility with other shells (like ksh) and might not significantly alter behavior in Bash as it always checks functions first.

-p
    Force a PATH search for name, even if it's an alias, function, or built-in. If found, the full pathname of the executable is printed. If not found, nothing is printed and the exit status is non-zero.

-P
    Similar to -p, but restricts the output to only the first executable found in the PATH, regardless of whether it's an alias, function, or built-in. If found, the full pathname is printed.

-t
    Output a single word describing the type of name: alias, keyword, function, builtin, or file (for an external executable). This is useful for programmatic use or scripting.

DESCRIPTION

The type command is a shell built-in used to describe how a command line argument (a 'command name') would be interpreted by the current shell. It reveals whether the name refers to an alias, a shell function, a shell built-in command, or an external executable program found in the system's PATH. This command is invaluable for debugging shell scripts, understanding command precedence, and ensuring that the correct version of a command is being invoked in a specific shell environment. Unlike tools like which, type provides a comprehensive view of the shell's command lookup order, including its internal mechanisms.

CAVEATS

The type command is a shell built-in, meaning its exact behavior and available options can vary slightly between different POSIX-compliant shells (e.g., Bash, Zsh, Ksh).
It reflects the command interpretation of the current shell environment and does not provide information about how a command might be interpreted in a different shell or user's environment.
The output format can differ significantly based on whether options like -t, -p, or -a are used.

COMMAND PRECEDENCE

The type command is fundamental to understanding the shell's command precedence rules. When you type a command, the shell typically looks for it in the following order:
1. Aliases
2. Keywords (like if, for, while)
3. Functions
4. Built-in commands (like cd, pwd)
5. External executables found in the directories specified by the PATH environment variable.
type helps reveal which of these categories a given command name falls into.

EXIT STATUS

The type command returns an exit status of 0 if all specified name arguments are found and recognized. It returns a non-zero status (typically 1) if one or more name arguments are not found or recognized by the shell. This makes it suitable for use in conditional statements within shell scripts.

HISTORY

The type command has been a standard feature in POSIX-compliant shells for a long time. Its inclusion is crucial for shell developers and users to understand the intricate command lookup mechanisms. It provides a programmatic way to query the shell's internal decision-making process when interpreting command names, making it an indispensable tool for shell scripting and debugging. Its design reflects the need to differentiate between various command sources (aliases, functions, built-ins, and external binaries) that can share the same name.

SEE ALSO

which(1), command(1), declare(1), alias(1)

Copied to clipboard