command - Linux


Overview

The command utility in Linux is used to run a command with a name given by the user, bypassing any shell function named the same. This ensures that built-in shell commands can be accessed even when a function with the same name exists. It is most effectively used in scripting and situations where function override conflicts occur.

Syntax

The basic syntax of the command command is:

command [-pVv] command [arguments...]
  • command: The name of the command you want to execute.
  • arguments...: Arguments or options passed to the command.

Options

The command command accommodates several options that modify its behavior:

  • -p: Use a default value for PATH that is guaranteed to find all the standard utilities.
  • -V: Print a description of command, similar to the type command but more concise.
  • -v: Print a description of command, similar to the type command but more concise.

Examples

  1. Basic Usage:
    Run the ls command using command, bypassing any aliases or functions named ls:

    command ls
    
  2. Using with options and arguments:
    Bypass the overridden echo function to use the built-in echo:

    command echo "Hello, World!"
    
  3. Check the command location with detailed output:
    To find out how a command would be interpreted by the shell without executing it:

    command -V ls
    
  4. Force standard utilities PATH:
    Use the standard path to ensure using the default grep instead of a custom one:

    command -p grep "pattern" file.txt
    

Common Issues

  • Function override not bypassed: Users sometimes forget that alias bypassing does not apply to scripts unless they are run in a login shell.
  • Incorrect PATH settings: With -p, ensure that the PATH used does not include custom script locations which might still override the expected command behavior.

Integration

The command utility is useful in scripts where exact behavior of shell built-ins must be guaranteed without interference from user-defined functions:

Script example

#!/bin/bash
# A script to use default utilities to avoid function overrides
command -p rm myfile.txt
command -p ls -la /home/user

Combining with other tools

Check if a custom command or a built-in is used:

echo $(command -v ls)
  • type: Shows information about the type of command (alias, keyword, function, built-in, or file).
  • which: Locates a command and shows the full path of the executable.

For further reading, consult the official Linux documentation related to the shell you are using, as behavior might slightly differ (bash, zsh, etc.).