Command documentation sourced from the linux-command project This comprehensive command reference is part of the linux-command documentation project.
type - Command Type Identification
The type command is a built-in shell utility that displays information about command types and their locations. It helps identify whether a command is a shell builtin, an alias, a function, or an external executable file. This is particularly useful for debugging shell scripts, understanding command resolution, and troubleshooting environment issues. The command works with various shells including Bash, Zsh, and other POSIX-compliant shells, providing essential insights into how the shell interprets and executes commands.
Basic Syntax
type [OPTIONS] COMMAND_NAME...
type -t COMMAND_NAME...
type -a COMMAND_NAME...
type -p COMMAND_NAME...
type -f COMMAND_NAME...
Common Options
Display Options
-t- Show single word description: alias, builtin, file, function, or keyword-a- Show all locations containing the named executable-p- Return name of disk file that would be executed (or nothing if builtin)-f- Suppress shell function lookup (same as command builtin)-P- Force a PATH search for each COMMAND_NAME
Shell Behavior
--help- Display help information and exit--version- Output version information and exit
Command Types Identified
Builtins
Shell commands built into the shell itself:
alias,cd,echo,exit,export,historyjobs,kill,pwd,read,source,type,ulimitset,unset,shift,local,declare
Aliases
Custom shortcuts defined by the user or system:
ll→ls -alFla→ls -Al→ls -CFgrep→grep --color=auto
Functions
User-defined or shell-defined functions:
- Command-line completion functions
- Custom utility functions
- Shell profile functions
External Files
Executable programs in the system PATH:
/bin/ls,/usr/bin/vim,/usr/local/bin/git- Scripts and compiled binaries
Keywords
Shell reserved words:
if,then,else,fi,for,while,case,esacfunction,time,coproc,select
Usage Examples
Basic Command Type Queries
Single Command Analysis
# Check basic command type
type ls
# Output: ls is aliased to 'ls --color=auto'
# Check builtin command
type cd
# Output: cd is a shell builtin
# Check external command
type git
# Output: git is /usr/local/bin/git
# Check function
type -t my_function
# Output: function (if defined)
# Check shell keyword
type if
# Output: if is a shell keyword
Multiple Commands
# Check multiple commands at once
type ls cd git vim
# Output:
# ls is aliased to 'ls --color=auto'
# cd is a shell builtin
# git is /usr/local/bin/git
# vim is /usr/bin/vim
# Check command types with -t option
type -t ls cd git vim if
# Output: alias builtin file file keyword
Advanced Command Discovery
Finding All Command Locations
# Show all occurrences of a command
type -a python
# Output:
# python is /usr/bin/python
# python is /usr/local/bin/python3
# python is /opt/python/bin/python3.9
# Find all instances of ls (including system overrides)
type -a ls
# Output:
# ls is aliased to 'ls --color=auto'
# ls is /usr/local/bin/ls
# ls is /usr/bin/ls
# Check for function overrides
type -a cd
# Output:
# cd is a shell builtin
# cd is a function (if user-defined)
Path-Only Searches
# Get executable path only
type -p git
# Output: /usr/local/bin/git
# Multiple path searches
type -p git python node
# Output:
# /usr/local/bin/git
# /usr/bin/python
# /usr/local/bin/node
# Check if command is external file
type -p cd
# Output: (empty, because cd is builtin)
Function and Alias Investigation
Function Analysis
# Define a sample function
my_function() {
echo "Hello from my_function"
}
# Check function details
type my_function
# Output:
# my_function is a function
# my_function ()
# {
# echo "Hello from my_function"
# }
# Check only function definition
type -f my_function
# Suppress function lookup (look for external file only)
type -f echo # Shows if external echo exists
Alias Investigation
# Show alias definition
type ll
# Output: ll is aliased to 'ls -alF'
# Check if alias exists with -t
type -t ll
# Output: alias
# Find all git-related aliases
alias | grep '^git' | while read alias_def; do
cmd_name=$(echo "$alias_def" | cut -d'=' -f1)
echo "Alias: $cmd_name"
type "$cmd_name"
done
Script Debugging and Development
Command Resolution Debugging
# Debug script command resolution
#!/bin/bash
# Check if required commands exist and their types
check_command() {
local cmd="$1"
if type -t "$cmd" >/dev/null 2>&1; then
echo "✓ $cmd: $(type -t "$cmd") - $(type -p "$cmd" 2>/dev/null || echo 'builtin/alias')"
return 0
else
echo "✗ $cmd: Not found"
return 1
fi
}
# Check required commands
for cmd in git python node npm; do
check_command "$cmd"
done
# Ensure we're using external binaries, not functions/aliases
external_git=$(type -p git)
if [ -n "$external_git" ]; then
echo "Using external git: $external_git"
"$external_git" --version
else
echo "Git not found in PATH"
fi
Environment Validation
# Validate development environment
validate_environment() {
echo "=== Environment Validation ==="
# Check critical commands
local critical_commands=("git" "node" "npm" "python" "make")
local missing_count=0
for cmd in "${critical_commands[@]}"; do
if cmd_type=$(type -t "$cmd" 2>/dev/null); then
if [ "$cmd_type" = "file" ]; then
echo "✓ $cmd: $(type -p "$cmd")"
else
echo "⚠ $cmd: $cmd_type (builtin/alias/function)"
fi
else
echo "✗ $cmd: Not found"
((missing_count++))
fi
done
echo "Missing commands: $missing_count"
return $missing_count
}
# Check for command conflicts
check_conflicts() {
echo "=== Command Conflict Detection ==="
local common_commands=("ls" "grep" "find" "cat")
for cmd in "${common_commands[@]}"; do
echo "Checking $cmd:"
type -a "$cmd" | while read line; do
echo " $line"
done
echo
done
}
System Administration Applications
Command Audit and Security
# System command audit
audit_system_commands() {
echo "=== System Command Audit ==="
# Critical system commands to audit
local critical_cmds=("sudo" "su" "passwd" "chmod" "chown" "usermod")
for cmd in "${critical_cmds[@]}"; do
echo "Auditing $cmd:"
# Check all locations
type -a "$cmd" | while read line; do
if [[ $line == */"$cmd" ]]; then
local cmd_path=$(echo "$line" | awk '{print $NF}')
echo " Path: $cmd_path"
echo " Permissions: $(ls -la "$cmd_path")"
echo " Checksum: $(sha256sum "$cmd_path" | cut -d' ' -f1)"
echo
fi
done
done
}
# Check for suspicious overrides
check_command_integrity() {
echo "=== Command Integrity Check ==="
# Commands that should be system binaries
local expected_external=("ls" "cat" "grep" "awk" "sed" "ps")
for cmd in "${expected_external[@]}"; do
cmd_type=$(type -t "$cmd")
if [ "$cmd_type" != "file" ]; then
echo "⚠ WARNING: $cmd is $cmd_type, not external file"
type -a "$cmd"
else
echo "✓ $cmd: $(type -p "$cmd")"
fi
done
}
Performance Analysis
# Command lookup performance analysis
benchmark_command_lookup() {
echo "=== Command Lookup Performance ==="
local commands=("git" "python" "node" "ls" "grep" "find")
for cmd in "${commands[@]}"; do
echo "Testing $cmd lookup:"
# Test type command performance
time for i in {1..100}; do
type -t "$cmd" >/dev/null
done
# Test type -p performance
time for i in {1..100}; do
type -p "$cmd" >/dev/null
done
echo
done
}
# PATH analysis
analyze_command_paths() {
echo "=== Command Path Analysis ==="
# Find most common command locations
echo "Top command directories:"
type -a git python node java go rust cargo ruby perl 2>/dev/null | \
grep -E 'is .*/' | \
awk '{print $NF}' | \
xargs dirname | \
sort | \
uniq -c | \
sort -nr | \
head -10
echo
echo "All commands in your PATH:"
IFS=':' read -ra PATH_DIRS <<< "$PATH"
for dir in "${PATH_DIRS[@]}"; do
if [ -d "$dir" ]; then
echo "$dir: $(ls -1 "$dir" | wc -l) commands"
fi
done
}
Integration and Automation
Shell Script Utilities
Command Validation Functions
#!/bin/bash
# Command validation utility functions
# Check if command exists and return type
command_exists() {
local cmd="$1"
local expected_type="${2:-file}" # Default: expect external file
if type -t "$cmd" >/dev/null 2>&1; then
local actual_type=$(type -t "$cmd")
if [ "$actual_type" = "$expected_type" ] || [ "$expected_type" = "any" ]; then
return 0
fi
fi
return 1
}
# Get command path with fallback
get_command_path() {
local cmd="$1"
local fallback="$2"
local path=$(type -p "$cmd" 2>/dev/null)
if [ -n "$path" ]; then
echo "$path"
else
echo "$fallback"
fi
}
# Validate multiple commands
validate_dependencies() {
local -a required=("$@")
local -a missing=()
for cmd in "${required[@]}"; do
if ! command_exists "$cmd"; then
missing+=("$cmd")
fi
done
if [ ${#missing[@]} -eq 0 ]; then
echo "All dependencies found"
return 0
else
echo "Missing dependencies: ${missing[*]}"
return 1
fi
}
# Usage example
# if validate_dependencies "git" "python" "node"; then
# echo "Proceeding with installation..."
# fi
Environment Setup Script
#!/bin/bash
# Environment setup with command validation
setup_development_environment() {
echo "Setting up development environment..."
# Define required tools
declare -A tools=(
["git"]="file"
["node"]="file"
["npm"]="file"
["python"]="file"
["pip"]="file"
["make"]="file"
["gcc"]="file"
)
local missing_count=0
local wrong_type_count=0
for tool in "${!tools[@]}"; do
expected_type="${tools[$tool]}"
if type -t "$tool" >/dev/null 2>&1; then
actual_type=$(type -t "$tool")
if [ "$actual_type" = "$expected_type" ]; then
if [ "$expected_type" = "file" ]; then
echo "✓ $tool: $(type -p "$tool")"
else
echo "✓ $tool: $actual_type"
fi
else
echo "⚠ $tool: Expected $expected_type, found $actual_type"
((wrong_type_count++))
fi
else
echo "✗ $tool: Not found"
((missing_count++))
fi
done
# Check for command conflicts
echo -e "\n=== Checking for command conflicts ==="
for tool in git node python; do
locations=$(type -a "$tool" 2>/dev/null | grep -c "is /")
if [ "$locations" -gt 1 ]; then
echo "⚠ $tool found in $locations locations:"
type -a "$tool" | grep "is /"
fi
done
# Summary
echo -e "\n=== Setup Summary ==="
echo "Missing commands: $missing_count"
echo "Wrong type commands: $wrong_type_count"
if [ "$missing_count" -eq 0 ] && [ "$wrong_type_count" -eq 0 ]; then
echo "✓ Environment setup complete"
return 0
else
echo "✗ Environment setup incomplete"
return 1
fi
}
Interactive Tools
Command Information Dashboard
#!/bin/bash
# Interactive command information dashboard
show_command_info() {
local cmd="$1"
if ! type -t "$cmd" >/dev/null 2>&1; then
echo "Command '$cmd' not found"
return 1
fi
echo "=== Command Information: $cmd ==="
echo "Type: $(type -t "$cmd")"
# Show all locations
echo -e "\nLocations:"
type -a "$cmd" | while read line; do
echo " $line"
done
# If external file, show additional info
local cmd_path=$(type -p "$cmd" 2>/dev/null)
if [ -n "$cmd_path" ]; then
echo -e "\nFile Information:"
echo " Path: $cmd_path"
echo " Size: $(du -h "$cmd_path" | cut -f1)"
echo " Modified: $(stat -c %y "$cmd_path" 2>/dev/null || stat -f %Sm "$cmd_path")"
echo " Permissions: $(ls -la "$cmd_path" | awk '{print $1 $3 $4}')"
# Show file type
echo " File type: $(file "$cmd_path")"
# Show symlink target if applicable
if [ -L "$cmd_path" ]; then
echo " Symlink to: $(readlink "$cmd_path")"
fi
fi
# Show man page location
local man_page=$(man -w "$cmd" 2>/dev/null)
if [ -n "$man_page" ]; then
echo -e "\nManual page: $man_page"
fi
}
# Interactive command browser
interactive_command_browser() {
while true; do
echo -e "\n=== Command Information Browser ==="
echo "Enter command name (or 'quit' to exit):"
read -p "> " cmd
case "$cmd" in
quit|exit|q)
echo "Goodbye!"
break
;;
"")
continue
;;
*)
show_command_info "$cmd"
;;
esac
done
}
Command Path Analyzer
#!/bin/bash
# Comprehensive command path analyzer
analyze_command_paths() {
local search_cmd="$1"
if [ -z "$search_cmd" ]; then
echo "Usage: $0 <command_name>"
return 1
fi
echo "=== Comprehensive Analysis: $search_cmd ==="
# Basic type information
echo "Basic Information:"
type "$search_cmd"
echo
# All occurrences
echo "All occurrences:"
type -a "$search_cmd"
echo
# External file path
local cmd_path=$(type -p "$search_cmd" 2>/dev/null)
if [ -n "$cmd_path" ]; then
echo "External file analysis:"
echo " Primary path: $cmd_path"
# Find all executables with same name
echo -e "\nAll files named '$search_cmd' in PATH:"
IFS=':' read -ra PATH_DIRS <<< "$PATH"
for dir in "${PATH_DIRS[@]}"; do
if [ -x "$dir/$search_cmd" ]; then
echo " $dir/$search_cmd"
fi
done
# File system analysis
echo -e "\nFile system details:"
echo " Size: $(stat -c %s "$cmd_path" 2>/dev/null || stat -f %z "$cmd_path") bytes"
echo " Owner: $(stat -c %U "$cmd_path" 2>/dev/null || stat -f %Su "$cmd_path")"
echo " Group: $(stat -c %G "$cmd_path" 2>/dev/null || stat -f %Sg "$cmd_path")"
echo " Permissions: $(stat -c %A "$cmd_path" 2>/dev/null || stat -f "%A" "$cmd_path")"
# Package information (if available)
if command -v dpkg >/dev/null 2>&1; then
local package=$(dpkg -S "$cmd_path" 2>/dev/null | cut -d: -f1)
if [ -n "$package" ]; then
echo " Package: $package"
fi
elif command -v rpm >/dev/null 2>&1; then
local package=$(rpm -qf "$cmd_path" 2>/dev/null)
if [ -n "$package" ]; then
echo " Package: $package"
fi
fi
else
echo "No external file found (builtin/alias/function)"
fi
# Shell-specific information
echo -e "\nShell information:"
if [ -n "$BASH_VERSION" ]; then
echo " Shell: Bash $BASH_VERSION"
# Check bash completion
if complete -p "$search_cmd" >/dev/null 2>&1; then
echo " Bash completion: Configured"
complete -p "$search_cmd"
fi
elif [ -n "$ZSH_VERSION" ]; then
echo " Shell: Zsh $ZSH_VERSION"
fi
# Check for aliases or functions
if [ "$(type -t "$search_cmd")" = "alias" ]; then
echo -e "\nAlias definition:"
alias "$search_cmd"
elif [ "$(type -t "$search_cmd")" = "function" ]; then
echo -e "\nFunction definition:"
type "$search_cmd" | tail -n +2
fi
}
Troubleshooting
Common Issues
Command Not Found Errors
# Debug command not found issues
debug_command_not_found() {
local cmd="$1"
echo "=== Debugging '$cmd' not found ==="
# Check if command exists in any form
if type -t "$cmd" >/dev/null 2>&1; then
echo "Command found, but might be:"
type -a "$cmd"
else
echo "Command not found in any form"
# Check PATH
echo -e "\nCurrent PATH:"
echo "$PATH" | tr ':' '\n' | nl
# Search for similar commands
echo -e "\nSimilar commands:"
IFS=':' read -ra PATH_DIRS <<< "$PATH"
for dir in "${PATH_DIRS[@]}"; do
if [ -d "$dir" ]; then
find "$dir" -name "*$cmd*" -type f -executable 2>/dev/null | head -5
fi
done
# Check for typos
echo -e "\nDid you mean one of these?"
compgen -c | grep -i "$cmd" | head -10
fi
}
# Fix common PATH issues
fix_path_issues() {
echo "=== PATH Diagnostics ==="
# Check for duplicate directories in PATH
echo "Duplicate PATH entries:"
echo "$PATH" | tr ':' '\n' | sort | uniq -d
# Check for non-existent directories
echo -e "\nNon-existent PATH directories:"
IFS=':' read -ra PATH_DIRS <<< "$PATH"
for dir in "${PATH_DIRS[@]}"; do
if [ ! -d "$dir" ]; then
echo " $dir"
fi
done
# Check directory permissions
echo -e "\nPATH directory permissions:"
IFS=':' read -ra PATH_DIRS <<< "$PATH"
for dir in "${PATH_DIRS[@]}"; do
if [ -d "$dir" ]; then
if [ -r "$dir" ] && [ -x "$dir" ]; then
echo " ✓ $dir (readable, executable)"
else
echo " ✗ $dir (permission issues)"
fi
fi
done
}
Command Resolution Conflicts
# Detect and resolve command conflicts
detect_command_conflicts() {
local cmd="$1"
if [ -z "$cmd" ]; then
echo "Usage: $0 <command_name>"
return 1
fi
echo "=== Command Conflict Detection: $cmd ==="
# Show all command occurrences
local locations=$(type -a "$cmd" 2>/dev/null | grep -c "is .*/")
if [ "$locations" -gt 1 ]; then
echo "⚠ WARNING: $cmd found in $locations locations"
type -a "$cmd"
echo -e "\nRecommendations:"
echo "1. Use absolute path for specific version"
echo "2. Remove unwanted versions from PATH"
echo "3. Use 'command' builtin to bypass functions/aliases"
echo "4. Use '\$cmd' to bypass aliases"
else
echo "✓ No conflicts found for $cmd"
fi
}
# Resolve command priority
show_command_priority() {
local cmd="$1"
echo "=== Command Resolution Priority: $cmd ==="
# Show resolution order
echo "Shell resolution order for '$cmd':"
echo "1. Shell keywords"
echo "2. Shell functions"
echo "3. Shell builtins"
echo "4. Aliases"
echo "5. External executables in PATH"
echo
# Show actual resolution
echo "Actual resolution:"
type "$cmd"
# Show what command would execute
echo -e "\nWhat executes when you run '$cmd':"
if command -v "$cmd" >/dev/null 2>&1; then
echo " $(command -v "$cmd")"
else
echo " Command not found"
fi
}
Performance Issues
# Diagnose command lookup performance
diagnose_performance() {
echo "=== Command Lookup Performance Diagnostics ==="
# Test PATH length impact
local path_entries=$(echo "$PATH" | tr ':' '\n' | wc -l)
echo "PATH entries: $path_entries"
# Test command lookup speed
local test_commands=("git" "python" "node" "ls" "grep")
echo -e "\nLookup speed test:"
for cmd in "${test_commands[@]}"; do
if command -v "$cmd" >/dev/null 2>&1; then
# Time the lookup
start_time=$(date +%s%N)
for i in {1..100}; do
type -t "$cmd" >/dev/null
done
end_time=$(date +%s%N)
duration=$(( (end_time - start_time) / 1000000 ))
echo " $cmd: ${duration}ms for 100 lookups"
fi
done
# Check for inefficient PATH entries
echo -e "\nInefficient PATH entries:"
IFS=':' read -ra PATH_DIRS <<< "$PATH"
for dir in "${PATH_DIRS[@]}"; do
if [ -d "$dir" ]; then
# Count executables
local exec_count=$(find "$dir" -maxdepth 1 -type f -executable 2>/dev/null | wc -l)
echo " $dir: $exec_count executables"
fi
done
}
Related Commands
which- Locate a command in PATHwhereis- Locate binary, source, and manual page filescommand- Execute command with special propertiesbuiltin- Execute shell builtinshash- Remember command locationscompgen- Display possible completionsalias- Create command aliasesdeclare- Declare shell variables and functionsexport- Set environment variables
Best Practices
- Use
type -tfor scripting to check command existence efficiently - Prefer
type -pwhen you need the actual file path - Use
commandbuiltin to bypass functions and aliases when needed - Check
type -ato identify command conflicts in troubleshooting - Use
type -fto suppress function lookup for external commands - Regular PATH audits to identify and resolve conflicts
- Use absolute paths in scripts for critical commands
- Validate dependencies before script execution
- Document command requirements in script headers
- Test in different shells for portable scripts
Performance Tips
type -tis faster thanwhichfor existence checking- Cache command paths for repeated use in scripts
- Minimize PATH entries to improve lookup speed
- Use hash table optimization with
hash -rto reset - Local PATH modifications are faster than global changes
- Avoid recursive function definitions that conflict with commands
- Use shell-specific optimizations for performance-critical scripts
- Profile command lookup in large automation scripts
- Consider using absolute paths for frequently called commands
- Batch command validation rather than individual checks
The type command is an essential diagnostic and development tool that provides comprehensive insights into command resolution, environment configuration, and shell behavior. Its ability to distinguish between builtins, aliases, functions, and external executables makes it invaluable for system administration, script development, and troubleshooting complex shell environments.