type Command

Display information about command types, showing whether commands are builtins, aliases, functions, or external executables.

Syntax

type [OPTIONS] command_name [command_name...]

The type command is a shell builtin that displays information about how command names would be interpreted if used as commands.

Common Options

Option Description
-a Display all locations containing command
-t Display single word describing type
-p Display path to executable (if external command)
-P Force PATH search (ignore aliases/functions)
-f Suppress shell function lookup

Basic Usage

Identifying command types

# Check if command is builtin type cd # Output: cd is a shell builtin type echo # Output: echo is a shell builtin # Check external commands type ls # Output: ls is /bin/ls type grep # Output: grep is /bin/grep # Check multiple commands type cd ls grep echo

Identify different types of commands

Command type categories

# Shell builtins type cd pwd echo test # External executables type ls cat grep awk # Check aliases (if any exist) alias ll='ls -la' type ll # Output: ll is aliased to `ls -la' # Check functions (if any exist) myfunction() { echo "Hello"; } type myfunction # Output: myfunction is a function

Different categories of commands that type can identify

Advanced Options

Show all locations

# Show all possible locations type -a python # Output might show: # python is /usr/bin/python # python is /usr/local/bin/python type -a echo # Output: # echo is a shell builtin # echo is /bin/echo # Show all locations for common commands type -a ls cat grep

Display all possible locations where a command might be found

Type-only output

# Get just the type type -t cd # Output: builtin type -t ls # Output: file type -t ll # (if aliased) # Output: alias type -t myfunction # (if function exists) # Output: function # Use in scripts for conditional logic if [ "$(type -t git)" = "file" ]; then echo "Git is installed" fi

Get concise type information for scripting

Path-only output

# Get path to executable type -p ls # Output: /bin/ls type -p python # Output: /usr/bin/python # Returns nothing for builtins type -p cd # (no output) # Force PATH search (ignore aliases/functions) type -P echo # Output: /bin/echo

Extract just the path to executable files

Command Resolution

Understanding command precedence

Shell Command Resolution Order
  1. Aliases - Custom command shortcuts
  2. Keywords - Shell reserved words (if, for, while)
  3. Functions - User-defined shell functions
  4. Builtins - Commands built into the shell
  5. External commands - Executable files in PATH

Examining command resolution

# Create alias and see precedence alias ls='ls --color=auto' type ls # Output: ls is aliased to `ls --color=auto' # Show all possibilities type -a ls # Output: # ls is aliased to `ls --color=auto' # ls is /bin/ls # Force PATH search (skip alias) type -P ls # Output: /bin/ls # Remove alias and check again unalias ls type ls # Output: ls is /bin/ls

Understand how the shell resolves command names

Functions vs builtins vs externals

# Create a function with same name as builtin cd() { echo "Custom cd function" builtin cd "$@" } type cd # Output: cd is a function type -a cd # Output: # cd is a function # cd is a shell builtin # Suppress function lookup type -f cd # Output: cd is a shell builtin # Remove function unset -f cd type cd # Output: cd is a shell builtin

Examine interactions between functions, builtins, and external commands

Practical Examples

System administration

# Check if commands are available type -p systemctl && echo "systemd system" type -p service && echo "SysV init system" # Verify package manager type -p apt && echo "Debian/Ubuntu system" type -p yum && echo "RHEL/CentOS system" type -p pacman && echo "Arch system" # Check development tools type -p gcc && echo "GCC compiler available" type -p make && echo "Make build tool available" type -p git && echo "Git version control available"

Use type for system administration and environment checking

Scripting and automation

#!/bin/bash # Script to check required commands REQUIRED_COMMANDS="curl wget git make gcc" for cmd in $REQUIRED_COMMANDS; do if type -p "$cmd" > /dev/null 2>&1; then echo "✓ $cmd is available at $(type -p "$cmd")" else echo "✗ $cmd is not available" exit 1 fi done # Check if command is builtin check_builtin() { if [ "$(type -t "$1")" = "builtin" ]; then echo "$1 is a shell builtin" return 0 else echo "$1 is not a builtin" return 1 fi } check_builtin echo check_builtin ls

Use type in scripts for dependency checking

Development environment setup

# Check programming language interpreters type -p python3 && python3 --version type -p node && node --version type -p ruby && ruby --version type -p java && java -version # Check editors and IDEs type -p vim && echo "Vim editor available" type -p emacs && echo "Emacs editor available" type -p code && echo "VS Code available" # Check version control type -p git && git --version type -p svn && svn --version # Check build tools type -p cmake && cmake --version type -p ninja && ninja --version

Verify development environment setup

type vs which vs whereis

Command Comparison
type
  • Shell builtin
  • Shows all command types
  • Respects aliases/functions
  • Fast and comprehensive
which
  • External command
  • Only finds executables
  • Searches PATH only
  • Portable across systems
whereis
  • External command
  • Finds binaries, sources, manuals
  • Searches standard locations
  • More comprehensive search

Comparison examples

# Create an alias for comparison alias ls='ls --color=auto' # type shows the alias type ls # Output: ls is aliased to `ls --color=auto' # which shows the executable which ls # Output: /bin/ls # whereis shows multiple locations whereis ls # Output: ls: /bin/ls /usr/share/man/man1/ls.1.gz # For builtins type cd # Output: cd is a shell builtin which cd # Output: (nothing - cd is not in PATH) whereis cd # Output: cd: /usr/share/man/man1/cd.1.gz

Compare different command location tools

Troubleshooting

Common issues and solutions

# Command not found type nonexistent # Output: bash: type: nonexistent: not found # Check if command exists before using if type -p git > /dev/null 2>&1; then git status else echo "Git is not installed" fi # Debug PATH issues echo $PATH type -a python # Check for conflicting aliases type -a ls alias | grep ls # Reset command to default unalias ls # Remove alias unset -f ls # Remove function type ls # Should show external command

Troubleshoot command resolution issues

Debugging shell behavior

# Check what shell you're using type -p bash type -p zsh echo $SHELL # Verify builtin commands type -t cd echo pwd test # Check for shell-specific builtins type -t pushd popd dirs # bash-specific # Debug function definitions type myfunction declare -f myfunction # Show function definition # Check command hash table hash -l # List hashed commands hash -r # Clear hash table

Debug shell command resolution and behavior

Best Practices

type Usage Best Practices
  • Use type -p in scripts to check for external commands
  • Use type -t for conditional logic based on command type
  • Use type -a to see all possible command locations
  • Prefer type over which for comprehensive checking
  • Use type to debug unexpected command behavior
  • Check command availability before using in scripts
Common Pitfalls
  • Shell dependency - type is a builtin, behavior may vary between shells
  • Alias masking - Aliases can hide the real command location
  • Function precedence - Functions take precedence over external commands
  • PATH changes - Command locations may change if PATH is modified
  • Hash table - Shell may cache command locations

See also