Keyboard shortcuts

Press ← or → to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

type Command Guide

type tells you what a command name really is: a shell builtin, an alias, a function, an external executable, or a combination. It’s the most honest answer to “what happens when I type this?” — more complete than which because it sees everything the shell sees.

What is type, and Why Use It?

which has a blind spot: it only looks for files in PATH. But the shell resolves names in a specific order — aliases first, then functions, then builtins, then PATH — and type shows you the whole picture:

type cd
# cd is a shell builtin

type ls
# ls is aliased to `ls --color=auto'

type python3
# python3 is /usr/bin/python3

type -a ls
# ls is aliased to `ls --color=auto'
# ls is /bin/ls
# ls is /usr/bin/ls   (on merged-usr systems)

When a command “does the wrong thing,” type is usually the first tool that explains why.

The Four Kinds of Commands

Every name you type resolves to one of these, in this precedence order:

  1. Alias — a text substitution (alias ll='ls -la')
  2. Function — a shell function defined in a script or rc file
  3. Builtin — a command built into the shell (cd, echo, export)
  4. File — an executable found in PATH
# See which kind you're dealing with
type export      # export is a shell builtin
type grep        # grep is /usr/bin/grep (or /bin/grep)
type myfunc      # myfunc is a function (if you defined one)
type ll          # ll is aliased to ... (if your distro defines it)

-a: Show Everything

By default type shows only the first match — the one that wins. The -a flag shows all matches, which is how you discover shadowing:

# Is there both a builtin and a binary?
type -a echo
# echo is a shell builtin
# echo is /bin/echo
# echo is /usr/bin/echo

# All copies of python in PATH
type -a python3

This is exactly how you answer “which version wins?” — the first one listed is what runs. Compare with the PATH page.

-p: Force a PATH-Only Lookup

Sometimes you want to skip the alias/builtin layer and ask “is there a file for this?” — that’s -p:

type -p cd        # prints nothing — cd has no file
type -p ls        # /usr/bin/ls (or similar)
type -p python3   # the file, ignoring any alias

type -p behaves like a smarter which: it only reports real files, and it prints nothing (with a nonzero exit) when the name is only a builtin.

-f: Show the Full Path of Functions

For functions, -f prints the source where the function is defined:

type -f myfunc
# myfunc is a function
# /home/you/.bashrc

Handy when you’ve defined a function and forgotten where — or when a sourced script defines one you didn’t know about.

type in Scripts

type is a builtin, so it’s available in every shell script without installing anything:

# Check that a command exists as a file
if type -p curl > /dev/null; then
  curl https://example.com
fi

# Distinguish builtin from external (matters for portability)
if type git > /dev/null 2>&1; then
  echo "git available"
fi

The POSIX-standard equivalent is command -v, which works even in strict POSIX shells:

command -v python3

type vs which vs hash

QuestionTool
“What is this name?”type
“Which file would run?”which
“Why is my shell remembering the old path?”hash / hash -r

After installing or moving a binary, bash caches PATH lookups. type always reflects current reality; which (the external program) doesn’t cache, but the shell’s own resolution does:

hash -r          # clear bash's command cache
type -a newcmd   # now shows the freshly installed location

Common Pitfalls

  • type is a builtin, not just a file. There’s usually no /usr/bin/type — scripts must run in a shell that has it (bash, zsh, and most modern shells do; pure POSIX sh may not).
  • Aliases need interactive shells. In a script, type ll may report nothing because aliases aren’t expanded in non-interactive shells unless you shopt -s expand_aliases. See aliases.
  • -a output order matters. The first line is the winner; don’t scroll to the bottom looking for the path that runs.
  • Functions can hide binaries. If someone defines function ls { ... }, type -a ls shows the function first. type -p ls skips to the file.
  • Exit codes. type exits nonzero when the name is unknown — useful in scripts, surprising in interactive use.
  • which — find the executable in PATH
  • whereis — binary, source, and man page locations
  • aliases — the alias layer type sees first
  • PATH — the search order for files
  • bash-sh-zsh — builtins vs binaries per shell
  • history — revisit the commands that defined your functions

Cheatsheet

CommandWhat it does
type cd“cd is a shell builtin”
type -a lsEvery resolution: alias, builtin, all PATH copies
type -p cdNothing — cd has no file
type -p python3The file that would run, ignoring aliases
type -f myfuncWhere a function is defined
command -v cmdPOSIX equivalent, works everywhere
hash -rClear bash’s cached PATH lookups