command Command
Execute commands while bypassing shell functions and aliases, ensuring you run the actual binary or built-in command.
Syntax
command [OPTIONS] COMMAND [ARGUMENTS...]
command -v COMMAND
command -V COMMAND
The command command runs commands while bypassing shell functions and aliases, providing access to the original command implementation.
Common Options
| Option | Description |
|---|---|
-p |
Use default PATH, ignoring custom PATH |
-v |
Display path of command (like which) |
-V |
Display verbose information about command |
Basic Usage
Bypass aliases and functions
# Create an alias
alias ls='ls --color=auto -la'
# Use the alias
ls
# Bypass the alias
command ls
# Create a function
cd() {
builtin cd "$@" && ls
}
# Use the function
cd /tmp
# Bypass the function
command cd /tmp
Execute original commands bypassing customizations
Find command location
# Show command path (like which)
command -v ls
command -v grep
command -v python3
# Show detailed command information
command -V ls
command -V cd
command -V alias
# Check if command exists
if command -v docker >/dev/null 2>&1; then
echo "Docker is installed"
else
echo "Docker is not installed"
fi
Locate and verify command availability
Use default PATH
# Show current PATH
echo $PATH
# Use command with custom PATH
export PATH="/custom/bin:$PATH"
command -v ls
# Use command with default PATH
command -p -v ls
# Execute with default PATH
command -p ls /bin
Execute commands using system default PATH
Advanced Usage
Script safety
#!/bin/bash
# Ensure we use actual commands, not aliases
safe_copy() {
local src="$1"
local dest="$2"
# Use actual cp command, not any alias
command cp "$src" "$dest"
}
# Ensure we use actual rm command
safe_remove() {
local file="$1"
# Confirm before deletion
read -p "Delete $file? (y/N): " confirm
if [[ $confirm =~ ^[Yy]$ ]]; then
command rm "$file"
fi
}
# Use functions
safe_copy "source.txt" "backup.txt"
safe_remove "temp.txt"
Write safer scripts by bypassing aliases
Command verification
#!/bin/bash
check_command() {
local cmd="$1"
if command -v "$cmd" >/dev/null 2>&1; then
echo "✓ $cmd is available at: $(command -v "$cmd")"
command -V "$cmd"
else
echo "✗ $cmd is not available"
return 1
fi
echo
}
# Check required commands
required_commands=("git" "docker" "python3" "node" "npm")
echo "Checking required commands..."
for cmd in "${required_commands[@]}"; do
check_command "$cmd"
done
# Check optional commands
optional_commands=("vim" "emacs" "code" "curl" "wget")
echo "Checking optional commands..."
for cmd in "${optional_commands[@]}"; do
check_command "$cmd" || echo "Consider installing $cmd"
done
Verify command availability in scripts
Environment debugging
#!/bin/bash
debug_command() {
local cmd="$1"
echo "=== Debugging command: $cmd ==="
# Check if it's an alias
if alias "$cmd" 2>/dev/null; then
echo "Type: Alias"
alias "$cmd"
fi
# Check if it's a function
if declare -f "$cmd" >/dev/null 2>&1; then
echo "Type: Function"
declare -f "$cmd"
fi
# Check if it's a built-in
if type -t "$cmd" 2>/dev/null | grep -q "builtin"; then
echo "Type: Built-in"
fi
# Show actual command location
echo "Actual command location:"
command -V "$cmd"
# Show what would be executed
echo "What gets executed:"
type "$cmd"
echo "========================"
}
# Debug common commands
debug_command "ls"
debug_command "cd"
debug_command "grep"
Debug command resolution and execution
Practical Examples
System administration scripts
#!/bin/bash
# System backup script using command to ensure reliability
backup_system() {
local backup_dir="/backup/$(date +%Y%m%d)"
# Ensure we use actual commands
command mkdir -p "$backup_dir"
# Backup important directories
echo "Backing up /etc..."
command tar -czf "$backup_dir/etc.tar.gz" /etc
echo "Backing up /home..."
command tar -czf "$backup_dir/home.tar.gz" /home
echo "Backing up /var/log..."
command tar -czf "$backup_dir/logs.tar.gz" /var/log
# Set permissions
command chmod 600 "$backup_dir"/*.tar.gz
# Verify backups
echo "Backup files created:"
command ls -lh "$backup_dir"
# Clean old backups (keep last 7 days)
command find /backup -name "20*" -type d -mtime +7 -exec rm -rf {} \;
echo "Backup completed: $backup_dir"
}
# Check prerequisites
for cmd in mkdir tar chmod ls find rm; do
if ! command -v "$cmd" >/dev/null 2>&1; then
echo "Error: Required command '$cmd' not found"
exit 1
fi
done
backup_system
Reliable system administration scripts
Development environment setup
#!/bin/bash
setup_dev_environment() {
echo "Setting up development environment..."
# Check required tools
required_tools=("git" "curl" "wget" "unzip" "tar")
for tool in "${required_tools[@]}"; do
if ! command -v "$tool" >/dev/null 2>&1; then
echo "Installing $tool..."
case $(command -v apt-get || command -v yum || command -v brew) in
*apt-get*)
sudo apt-get update && sudo apt-get install -y "$tool"
;;
*yum*)
sudo yum install -y "$tool"
;;
*brew*)
brew install "$tool"
;;
*)
echo "Please install $tool manually"
;;
esac
else
echo "✓ $tool is already installed"
fi
done
# Setup Git if not configured
if ! command git config --global user.name >/dev/null 2>&1; then
read -p "Enter your Git username: " git_user
read -p "Enter your Git email: " git_email
command git config --global user.name "$git_user"
command git config --global user.email "$git_email"
fi
# Create development directories
command mkdir -p ~/dev/{projects,tools,scripts}
echo "Development environment setup complete!"
}
setup_dev_environment
Setup development environments reliably
Security-focused scripts
#!/bin/bash
secure_file_operations() {
local source_file="$1"
local dest_dir="$2"
# Validate inputs
if [[ ! -f "$source_file" ]]; then
echo "Error: Source file does not exist"
return 1
fi
if [[ ! -d "$dest_dir" ]]; then
echo "Error: Destination directory does not exist"
return 1
fi
# Use command to ensure we use actual binaries
echo "Copying file securely..."
# Copy with verification
if command cp "$source_file" "$dest_dir/"; then
echo "File copied successfully"
# Verify copy
if command cmp "$source_file" "$dest_dir/$(basename "$source_file")" >/dev/null 2>&1; then
echo "Copy verified successfully"
# Set secure permissions
command chmod 600 "$dest_dir/$(basename "$source_file")"
# Show final permissions
command ls -l "$dest_dir/$(basename "$source_file")"
else
echo "Error: Copy verification failed"
command rm "$dest_dir/$(basename "$source_file")"
return 1
fi
else
echo "Error: Copy operation failed"
return 1
fi
}
# Usage
secure_file_operations "sensitive.txt" "/secure/backup"
Security-focused file operations
Best Practices
command Usage Best Practices
- Use command in scripts to ensure predictable behavior
- Check command availability before using in scripts
- Use command -v for portable command detection
- Combine with error checking for robust scripts
- Use command -p for system administration scripts
- Document when and why you bypass aliases/functions
- Test scripts in different environments
Important Considerations
- Portability - command behavior may vary between shells
- Performance - Slight overhead compared to direct execution
- Debugging - May hide useful aliases in interactive use
- User expectations - Users may expect their customizations
- Built-ins - command doesn't bypass shell built-ins