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 forPATH
that is guaranteed to find all the standard utilities.-V
: Print a description ofcommand
, similar to the type command but more concise.-v
: Print a description ofcommand
, similar to the type command but more concise.
Examples
-
Basic Usage:
Run thels
command usingcommand
, bypassing any aliases or functions namedls
:command ls
-
Using with options and arguments:
Bypass the overriddenecho
function to use the built-inecho
:command echo "Hello, World!"
-
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
-
Force standard utilities PATH:
Use the standard path to ensure using the defaultgrep
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)
Related Commands
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.).