command

shell builtinsLinux/Unix
The command command is one of the most frequently used commands in Linux/Unix-like operating systems. command Run a command, ignoring shell functions and aliases

Quick Reference

Command Name:

command

Category:

shell builtins

Platform:

Linux/Unix

Basic Usage:

command [options] [arguments]

Common Use Cases

    Syntax

    command [-pVv] command [arg ...]

    Options

    Option Description
    -p Use a default value for PATH that is guaranteed to find all standard utilities
    -v Print a description of the command (the path to the command)
    -V Print a more verbose description of the command

    Examples

    How to Use These Examples

    The examples below show common ways to use the command command. Try them in your terminal to see the results. You can copy any example by clicking on the code block.

    #

    Basic Examples:

    # Run a command bypassing any aliases or functions with the same name
    command ls
    # Find the exact path of a command command -v ls
    # Check if a command exists (returns 0 if found, 1 if not) command -v non_existent_command >/dev/null 2>&1 && echo "Exists" || echo "Does not exist"
    # Display all details about a command command -V ls

    Advanced Examples:

    # Use in scripts to ensure running the actual command, not an alias
    command grep "pattern" file.txt
    # Verify a command exists before using it in a script if command -v curl >/dev/null 2>&1; then curl -s https://example.com else echo "curl is not installed" exit 1 fi
    # Force the use of a program even if there's a shell function with the same name function echo() { printf "Custom echo function\n"; } echo "This uses the function" command echo "This uses the real echo command" # Use with PATH modification to find commands in specific locations PATH=/custom/path:$PATH command -v program

    Try It Yourself

    Practice makes perfect! The best way to learn is by trying these examples on your own system with real files.

    Understanding Syntax

    Pay attention to the syntax coloring: commands, options, and file paths are highlighted differently.

    Notes

    The command utility allows you to run a command bypassing any shell functions or aliases with the same name. It's a shell builtin in most modern shells including bash, zsh, and ksh.

    Key points about command:

    • It's primarily used to bypass aliases and shell functions
    • With -v option, it's commonly used to check if a command exists
    • Unlike 'which' or 'type', it's built into the shell and works more consistently across systems
    • It's particularly useful in shell scripts to ensure predictable behavior
    • The command follows the same rules as regular command lookups (searches PATH, etc.)

    Common use cases:

    • Testing for the existence of a command in shell scripts
    • Bypassing aliases in interactive sessions or scripts
    • Finding the location of a command (similar to 'which')
    • Getting detailed information about a command (with -V)
    • Ensuring predictable behavior in scripts by ignoring user customizations

    Comparison with similar commands:

    • type: Shows how a command would be interpreted if used, also a shell builtin
    • which: External command that finds executables in PATH
    • whereis: Locates the binary, source, and manual page files for a command
    • command -v: Preferred way to test for command existence in scripts (POSIX-compliant)

    Note that command with the -p option uses a default, safe path to find standard system utilities, which can be useful in security-sensitive environments.

    Related Commands

    These commands are frequently used alongside command or serve similar purposes:

    Use Cases

    Learn By Doing

    The best way to learn Linux commands is by practicing. Try out these examples in your terminal to build muscle memory and understand how the command command works in different scenarios.

    $ command
    View All Commands