type Command in Linux: Show Command Type and Location

By 

•

Updated on

•

7 min read

Using the type command to show how the shell resolves a command name

You run grep and the matches come out highlighted, even though you never passed --color. Or you install a newer version of a tool in /usr/local/bin, and the shell keeps running the old one. In both cases, the name you typed does not resolve to what you expected: it may be an alias, a shell function, a builtin, or a path the shell remembered from an earlier run.

The type command shows how the current shell would interpret a name entered on the command line. Depending on the shell, it can identify aliases, functions, builtins, keywords, and executable files.

This article explains how to use the type builtin, with Bash examples and notes where Zsh and other popular shells behave differently.

How to Use the type Command

type is a shell builtin in Bash, Zsh, Ksh, and dash, but the supported options and output format are not identical across shells. The examples below use Bash syntax. You can check which options your current shell supports with help type, type --help, or the shell documentation.

The syntax for the type command is as follows:

txt
type [OPTIONS] NAME...

For example, to find the type of the wc command , you would type the following:

Terminal
type wc

The output will be something like this:

output
wc is /usr/bin/wc

If you have already run wc in the current session, Bash prints wc is hashed (/usr/bin/wc) instead. Bash remembers the full path of every command it runs, so it does not have to search PATH again. This is also why a newly installed version of a command can be ignored: the shell keeps using the remembered path until you clear it with hash -r.

You can also provide more than one argument to the type command:

Terminal
type sleep head

The output will include information about both sleep and head commands:

output
sleep is /usr/bin/sleep
head is /usr/bin/head

When the name cannot be found, type prints an error and returns a non-zero exit status:

Terminal
type nosuchcmd
output
bash: type: nosuchcmd: not found

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)

The -t option is specific to Bash. Zsh does not support it and fails with bad option: -t. The closest Zsh equivalent is type -w, which prints wc: command for files and for: reserved for keywords.

Here are a few examples:

  1. Alias

    Terminal
    type -t grep

    On Ubuntu, the default ~/.bashrc file sets grep as an alias for grep --color=auto:

    output
    alias

    Aliases from ~/.bashrc are loaded only in interactive shells, so the same command inside a script prints file.

  2. Function

    Terminal
    greet() { echo "Hello"; }
    type -t greet

    greet is a shell function defined in the current session:

    output
    function
  3. Builtin

    Terminal
    type -t echo

    echo is a shell builtin in Bash and other shells like Zsh and Ksh:

    output
    builtin
  4. File

    Terminal
    type -t cut

    cut is an executable file:

    output
    file
  5. Keyword

    Terminal
    type -t for

    for is a reserved word in Bash:

    output
    keyword

Without -t, type prints a full description of each name. For an alias, it shows the command the alias expands to, and for a function, it prints the whole function body:

Terminal
type grep greet
output
grep is aliased to `grep --color=auto'
greet is a function
greet ()
{
    echo "Hello"
}

This is useful when a command behaves strangely and you suspect that an alias or a function with the same name is running instead.

Display All Locations of a Command

To print all matches, use the -a option. This option is supported by Bash and Zsh, but the order and wording of the results may differ:

Terminal
type -a pwd

The output will show you that pwd is a shell builtin, but it is also available as a standalone executable:

output
pwd is a shell builtin
pwd is /usr/bin/pwd
pwd is /bin/pwd

On Ubuntu and Debian, /bin is a symlink to /usr/bin, and both directories are in the default PATH, so the same file is listed twice.

When -a is used without -p, type can include aliases and functions in addition to executable files.

Additional Options

In Bash, the -p option prints the path only when the resolved name is an executable file on disk. For example, the following command prints nothing because pwd resolves to a shell builtin:

Terminal
type -p pwd

The exit status is still 0, because type found the name. For a file, -p prints only the path, without the NAME is prefix:

Terminal
type -p wc
output
/usr/bin/wc

The uppercase -P option tells type to search PATH for an executable file on disk even when the name also resolves to a builtin or function:

Terminal
type -P pwd
output
/usr/bin/pwd

Both -p and -P can return a path from Bash’s hash table. To ignore cached paths and list executable files in PATH order, use type -ap name.

The -f option tells type to skip shell functions during lookup. Use it when a function shadows a command and you want to see what would run without the function.

Zsh handles these options differently. In Zsh, type -p forces a PATH search the same way Bash -P does, -P is not supported, and -f prints function definitions instead of skipping them.

Quick Reference

CommandDescription
type nameShow how a name would be interpreted
type name1 name2Check multiple names at once
type -t namePrint the type word only (alias, function, builtin, file, keyword); Bash only
type -a nameShow all locations that match the name
type -p namePrint the path only if it is a disk file (Bash)
type -P nameForce a PATH search even for builtins (Bash)
type -f nameIgnore shell functions when looking up the name (Bash)

FAQ

What is the difference between type and which?
The external which utility searches PATH for executable files. type is a shell builtin that also identifies aliases, functions, keywords, and other builtins, so it shows what the shell will actually run. Zsh provides a builtin which that also identifies aliases, functions, and builtins.

What does it mean when type says a command is a builtin?
The command is handled directly by the shell without forking a new process. Examples include echo, cd, pwd, and type itself.

Why does type -p return nothing for some commands?
In Bash, -p only prints a path when the command is an executable file on disk. Builtins, functions, aliases, and keywords produce no output with -p. Use -P to look for an external executable instead.

Does the type command behave the same in Bash and Zsh?
No. type is a shell builtin, so supported options and output format differ between Bash, Zsh, and other shells. For example, Zsh does not support -t or -P, and its -p option behaves like Bash -P. If you need a portable way to check whether a command exists in a script, prefer command -v name.

Can I use type in a script to check if a command exists?
In Bash, yes. Use type name > /dev/null 2>&1 and check the exit code. Exit code 0 means the command was found; a non-zero exit code means it was not. Keep in mind that #!/bin/sh scripts on Debian and Ubuntu run in dash, whose type does not accept options such as -t or -P. For portable scripts, use command -v, as shown in how to check if a command exists in Bash .

Conclusion

The type command shows how the shell will interpret a given name, whether it resolves to a builtin, alias, function, keyword, or an executable on disk. When a command behaves differently than expected, run type -a first to see every alias, function, builtin, and file that matches the name.

Linuxize Weekly Newsletter

A quick weekly roundup of new tutorials, news, and tips.

About the authors

Dejan Panovski

Dejan Panovski

Dejan Panovski is the founder of Linuxize, an RHCSA-certified Linux system administrator and DevOps engineer based in Skopje, Macedonia. Author of 1000+ Linux tutorials with 20+ years of experience turning complex Linux tasks into clear, reliable guides.

View author page