Sample output
grep is aliased to `grep --color=auto'
grep is /usr/bin/grep
Two matches. The shell runs the alias first, which wraps the real binary with --color=auto. If you did not know the alias existed, you might wonder why piping grep output into another tool includes ANSI color codes that break parsing.
When you would reach for it
A command behaves differently on your machine than on a colleague’s, or differently in a script than in your interactive shell. An alias or function is shadowing a binary and you cannot figure out why the flags you passed are being ignored. You installed a newer version of a tool and want to confirm the shell is picking up the right one.
Gotchas
typeis a shell built-in, so it sees aliases and functions defined in your current session. If you run it inside a script, it will not see the aliases from your.bashrcbecause non-interactive shells do not source that file by default.whichis not equivalent. On most systems,whichis an external binary that only searchesPATH. It does not know about aliases, functions, or built-ins.typeis almost always what you actually want.- In zsh,
typeis an alias forwhence -v. The output wording differs from bash (“grep is an alias for grep –color=auto” versus “grep is aliased to `grep –color=auto’”), but the information is the same. - If
type -ashows multiple file paths, the first one wins. This is how a Homebrew-installed GNU tool can shadow a BSD built-in on macOS, or vice versa.
Variants
$ type -t grep
Print only the type word: alias, function, builtin, file, or keyword. Useful in scripts that need to branch on whether a command exists and what kind it is
$ command -v grep
POSIX-portable alternative. Prints the path or alias definition, but with less detail than type. Use this in scripts that must run on /bin/sh
$ type -a ls grep awk sed
Check multiple commands at once. Each one gets its own block of output, so you can audit your whole toolchain in one line