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 renice Command: Change the Priority of a Running Process

Linux renice Command: Change the Priority of a Running Process

Learn how to change the Priority of a Running Process with the Linux renice command, including practical examples, key options, and important precautions.

Linux dpkg Command: Inspect and Install Local DEB Packages

Linux dpkg Command: Inspect and Install Local DEB Packages

Learn how to inspect and Install Local DEB Packages with the Linux dpkg command, including practical examples, key options, and important precautions.

Linux Tutorial / Linux Terminal and Shell Basics: Command Syntax and Finding Help

Linux Tutorial / Linux Terminal and Shell Basics: Command Syntax and Finding Help

Learn the difference between a terminal and a shell, read command syntax and output, and use Bash help tools through a short hands-on exercise.

Linux bzip2 Command: Compress a File in bzip2 Format

Linux bzip2 Command: Compress a File in bzip2 Format

Learn how to compress a File in bzip2 Format with the Linux bzip2 command, including practical examples, key options, and important precautions.

Linux who Command: List Logged-In Users

Linux who Command: List Logged-In Users

Learn how to list Logged-In Users with the Linux who command, including practical examples, key options, and important precautions.

Linux diff Command: Compare Text Files and Directories

Linux diff Command: Compare Text Files and Directories

Learn how Linux diff compares text files and directories, produces unified patches, ignores selected whitespace changes, and reports differences through exit status.

Linux printf Command: Print Values with a Defined Format

Linux printf Command: Print Values with a Defined Format

Learn how to print Values with a Defined Format with the Linux printf command, including practical examples, key options, and important precautions.

Linux awk Command: Analyze Text by Fields

Linux awk Command: Analyze Text by Fields

Learn how to analyze Text by Fields with the Linux awk command, including practical examples, key options, and important precautions.

Linux man Command: Read Command Manual Pages

Linux man Command: Read Command Manual Pages

Learn how to use the Linux man command to find and read command manuals, sections, options, and related pages, with essential options, practical examples, output interpretation, and common troubleshooting tips.

Linux nslookup Command: Query DNS Names and Records

Linux nslookup Command: Query DNS Names and Records

Learn how to query DNS Names and Records with the Linux nslookup command, including practical examples, key options, and important precautions.