Linux type Command: Identify How a Command Is Resolved

type is a built-in command in Bash that shows whether the command name you entered is interpreted as an alias, reserved word, function, built-in command, or executable file. It is useful for finding the cause when the same name exists in multiple forms or an unexpected program runs.

While which mainly focuses on finding executable files in the PATH, type explains the actual command resolution, including aliases and functions in the current shell. It is a good command to use first when diagnosing the Bash environment.

What is the type command?

Before executing a command name, the shell searches aliases, functions, built-in commands, and executable files in the PATH according to certain rules. type tells you what Bash currently considers that name to be and what it will execute.

type itself is a Bash built-in command. Other shells have similar commands, but options and output formats may differ, so the examples in this article are based on Bash.

Basic syntax and result types

type [-afptP] [name...]

You can specify one or more command names.

type ls
type cd
type if
type printf

The default output is a human-readable sentence. Depending on your environment settings, the following types may appear.

Type Meaning
alias A shell alias that expands to another string before input.
keyword Shell reserved words like if and for.
function Functions defined in the current shell.
builtin Built-in commands included in Bash itself.
file External executable files found through PATH search.

Check all candidates with the same name

-a prints all locations matching the name, including aliases, functions, built-in commands, and executables found in PATH.

type -a printf
type -a test
type -a ls

printf can exist as both a Bash built-in command and an external executable. The basic type printf only shows the first option that would be chosen, but -a is useful for examining candidates that are hidden behind.

Options for selecting output format

Print type only with -t

-t outputs one of alias, keyword, function, builtin, or file instead of a descriptive sentence. If the name is not found, it outputs nothing and returns a failure status.

type -t cd
type -t if
type -t ls

Print executable path with -p and -P

-p prints the executable path if the name is interpreted as an external file. It may not output anything if an alias, function, or built-in command is chosen first.

type -p ls
type -p cd

-P forces a PATH search to find an executable candidate even if the current name is an alias or built-in command.

type -P printf
type -P test

If Bash has a remembered hash path, -p and -P can show a stored path that differs from the first entry in the current PATH. If in doubt, clear the hash with hash -r and check again.

Summary of type options

Option Description
-a Print all candidates matching the name.
-f Skip searching for functions. If not used with -a, the effect may be limited.
-p Print the path when the default interpretation result is an external executable.
-P Find the executable in PATH with precedence over other types.
-t Print the command type as a single word.

When the command differs because of aliases and functions

Checking Aliases

In an interactive shell, frequently used options may be included in aliases.

type ls
alias ls

To execute the original name without applying an alias, you can use a backslash or an absolute path depending on the situation.

\ls
/usr/bin/ls

Checking Function Definitions

If a shell function is defined with the same name as an external command, the function may run first. The default output of type may display the function definition.

type deploy
type -t deploy

If you need to call an external command with the same name inside a function, you can use command name to bypass function lookup.

type, command -v, which Comparison

Method Features
type name It explains aliases, reserved words, functions, built-in commands, and files in Bash in detail.
type -a name Checks all possible candidates with the same name.
command -v name Relatively portable for checking command existence and resolution in shell scripts.
which name Depending on the implementation, it mainly finds executable files in PATH and may not accurately reflect shell functions or aliases.

For interactive diagnostics in Bash, type is generally suitable, and for checking command existence in POSIX shell scripts, command -v is usually appropriate.

Using safely in shell scripts

Branching depending on the type of command

kind=$(type -t "$1") || {
 printf 'Command not found: %s\n' "$1" >&2
 exit 1
}

case $kind in
 alias|function|builtin|file|keyword)
 printf '%s: %s\n' "$1" "$kind";;
esac

This example relies on Bash's type -t output, so make sure the script's interpreter is Bash.

Use exit status instead of output statements

The default output may vary depending on the version or locale. To check only the existence of a command, do not parse the result string; check the exit status.

if type git >/dev/null 2>&1; then
 printf '%s\n' 'git is available.'
fi

Frequently Asked Questions

type -p does not output anything

The name may first be interpreted as an alias, function, or built-in command, or there may be no external executable. Use type -a name to see all candidates, and use type -P name to force finding only the external file.

After changing PATH, the previous path appears

Previous execution paths may remain in Bash's hash table. Check the current hash and, if necessary, clear it before searching again.

hash
hash -r
type -P command

Aliases are not visible in scripts.

In non-interactive Bash, alias expansion is not enabled by default. Command interpretation results may differ between interactive shells and scripts, so it is recommended not to rely on aliases in automation.

type only finds the location of executable files?

No. It shows how Bash interprets the name currently and distinguishes aliases, reserved words, functions, and built-in commands.

Is the output order of type -a the execution priority?

The first item generally helps understand the target selected by the current shell. However, the hash, shell options, and execution context can have an impact, so for important tasks, check the actual environment together.

Do the options in other shells match?

It may not be like that. The options in this text are based on Bash. For Zsh, Dash, etc., please check the help for that shell.

Summary

type is a tool that distinguishes between aliases, functions, built-in commands, and executable files with the same name and diagnoses the actual command resolution. With the default output, you can read explanations, use -a to find all candidates, -t to get only the type, and -P to forcibly search for external executable files.

When a command behaves differently than expected, try running type -a name first. Other basic commands can be found in the list and overview of Linux commands.

Refer to official documents

More in This Category
Linux ethtool Command: Inspect Network Link and Driver Settings

Linux ethtool Command: Inspect Network Link and Driver Settings

Learn how to inspect Network Link and Driver Settings with the Linux ethtool command, including practical examples, key options, and important precautions.

Linux pgrep Command: Find Process IDs by Name or Criteria

Linux pgrep Command: Find Process IDs by Name or Criteria

Learn how to find Process IDs by Name or Criteria with the Linux pgrep command, including practical examples, key options, and important precautions.

Linux blkid Command: Find Block-Device UUIDs and Filesystem Types

Linux blkid Command: Find Block-Device UUIDs and Filesystem Types

Learn how to find Block-Device UUIDs and Filesystem Types with the Linux blkid command, including practical examples, key options, and important precautions.

Linux apk Command: Manage Packages on Alpine Linux

Linux apk Command: Manage Packages on Alpine Linux

Learn how to manage Packages on Alpine Linux with the Linux apk command, including practical examples, key options, and important precautions.

Linux kill Command: Send a Signal to a Process ID

Linux kill Command: Send a Signal to a Process ID

Learn how to send a Signal to a Process ID with the Linux kill command, including practical examples, key options, and important precautions.

Linux Tutorial / File System Basics: Paths and Key Directories

Linux Tutorial / File System Basics: Paths and Key Directories

Understand the Linux directory tree, read absolute and relative paths, and learn what common directories such as /home, /etc, and /var are used for.

Linux uptime Command: Check Uptime and Load Averages

Linux uptime Command: Check Uptime and Load Averages

Learn how to use the Linux uptime command to check how long the system has been running and interpret load averages, with essential options, practical examples, output interpretation, and common troubleshooting tips.

Linux bunzip2 Command: Decompress bzip2 Files

Linux bunzip2 Command: Decompress bzip2 Files

Learn how to decompress bzip2 Files with the Linux bunzip2 command, including practical examples, key options, and important precautions.

Linux Tutorial / Search and Process Text with grep, sort, uniq, cut, and wc

Linux Tutorial / Search and Process Text with grep, sort, uniq, cut, and wc

Practice searching, sorting, grouping, extracting, and counting text with grep, sort, uniq, cut, and wc, including reproducible output and key caveats.

Linux head Command: Display the Beginning of a File

Linux head Command: Display the Beginning of a File

Learn how Linux head prints the first lines or bytes of files and pipelines, labels multiple inputs, excludes a trailing portion, and handles multibyte data.