Shell builtins · bash builtin
type
Show how the shell would interpret a name, as an alias, keyword, function, builtin or file on PATH.
type [-afptP] NAME...
When you type a command name, bash checks, in order: aliases, shell keywords (if, [[),
functions, builtins, and finally the directories in PATH. type answers which of those a
name resolves to, and is the most accurate answer to "what actually runs when I type this?".
That makes it more useful than which, which only searches PATH and knows nothing about
aliases, functions or builtins. type cd says it is a builtin; which cd finds nothing or
points at a file that would never run. Use type -a to see every candidate in order of
precedence, which explains why a newly installed program is "ignored" (an alias or an earlier
PATH entry wins).
In scripts, type -t prints a single word (alias, keyword, function, builtin,
file) that is easy to test, and type -P forces a PATH lookup. For a portable "is this
command available?" check, command -v is the POSIX choice.
Examples
Find out what a name is
type ls cd if printf
ls is /usr/bin/ls cd is a shell builtin if is a shell keyword printf is a shell builtin
ls is a file in PATH, cd and printf are builtins, if is a keyword. The full
path shown for ls is the one that would run.
Print a function's definition
greet() { echo "Hello, ${1:-world}"; }
type greet
greet is a function
greet ()
{
echo "Hello, ${1:-world}"
} For a function, type prints the body as bash stores it, reformatted. A quick way to
inspect a function that some sourced file defined.
Reveal an alias
alias ll='ls -l'
type ll
shopt -s expand_aliases
type ll
/bin/bash: line 2: type: ll: not found ll is aliased to `ls -l'
type reports what would really happen. In a non-interactive shell aliases are not
expanded, so at first ll is "not found". Once expand_aliases is on, as it is in an
interactive terminal, type shows the alias and its value.
List every match with -a
type -a echo printf test
echo is a shell builtin echo is /usr/bin/echo echo is /bin/echo printf is a shell builtin printf is /usr/bin/printf printf is /bin/printf test is a shell builtin test is /usr/bin/test test is /bin/test
-a shows all the places a name is found, in precedence order. echo, printf and
test are builtins, but coreutils also installs them as files; the builtin is what runs.
Debian lists both /usr/bin and /bin because /bin is a symlink to /usr/bin.
Get a one-word answer with -t
greet() { :; }
for n in greet cd if grep nosuch; do
printf '%-7s %s\n' "$n" "$(type -t "$n")"
done
greet function cd builtin if keyword grep file nosuch
-t prints function, builtin, keyword, file or alias, and nothing for an
unknown name. Convenient in a case statement.
Print only the path with -p or -P
type -p grep
type -p cd; echo "-p on a builtin printed nothing"
type -P printf
/usr/bin/grep -p on a builtin printed nothing /usr/bin/printf
-p prints the file that would run, or nothing if the name is not a file. -P searches
PATH even when a builtin or function would take precedence.
An unknown name returns 1
type kubectl
/bin/bash: line 1: type: kubectl: not found
When none of the names are found, type prints an error and returns 1. With several
names, the status is 1 if any of them is missing.
See that bash remembered a path
grep -c TODO notes.txt
type grep
1 grep is hashed (/usr/bin/grep)
After a command runs, bash caches its location, and type reports it as hashed. If you
move a program, hash -r clears this cache.
Detect a function shadowing a command
ls() { echo "this is not the real ls"; }
type -t ls
ls
command ls scripts
function this is not the real ls backup.sh greet.sh
A function named like a command wins over the file in PATH. type exposes it, and
command skips functions to reach the real program.
Common pitfalls
Using
whichto debug "why does this command behave oddly". It ignores aliases and functions;type -ashows what actually runs.Testing
type foowithout silencing it in a script: it prints a line on success and an error on failure. Usetype foo >/dev/null 2>&1orcommand -v foo >/dev/null.type -pprints nothing for builtins, which can look like "not installed". Use-Pwhen you specifically need the file.
GNU vs BSD and macOS
The examples above ran the GNU/Linux version. These are the differences that matter on macOS and the BSDs (written from their documentation, not run here).
typeis required by POSIX, but its options are not; dash'stypehas none of-a,-t,-por-P. zsh supports-aand useswhencefor the rest. Usecommand -vfor portable checks.
Options worth knowing
A selection, described in our own words. For the full list, see bash(1), SHELL BUILTIN COMMANDS.
| Option | What it does |
|---|---|
-a | Show every alias, keyword, function, builtin and file matching the name. |
-t | Print a single word: alias, keyword, function, builtin or file. |
-p | Print the path of the file that would run, or nothing if it is not a file. |
-P | Search PATH for the name even when it is also a builtin or function. |
-f | Skip shell functions when looking up the name. |
Related
commandRun a command while skipping functions and aliases of the same name, or check whether a command exists with -v.whichShow which executable file in PATH would run for a command name.whereisLocate the binary, source and manual page files for a command in the standard system directories.aliasDefine a short name that the shell replaces with a longer command line, or list the aliases already defined.- FunctionsDefining and calling bash functions, arguments, local variables, return status versus output, namerefs, and sourcing a function library.
- Running scriptsThe four ways to start a script (./script, bash script, PATH, source), what each one changes, and the errors you get when one goes wrong.