Linux command Command: Run Commands While Bypassing Aliases and Functions

command is a shell builtin command that skips searching shell functions and runs either a builtin command or an external command found in PATH. It is mainly used to call the original command when a wrapper function with the same name is created, or to check with command -v whether the current shell can find the command.

In Bash, -v and -V are used to check command resolution information, and -p uses the default search path where standard utilities can be found. It has a different role from exec, env, and builtin, so it should be distinguished according to the purpose.

What is the command command?

A shell function can have the same name as an external command. In this case, calling the name directly inside the function can make it call itself again, leading to recursion. Prefixing it with command skips the shell function of the same name and selects the Bash builtin command or the executable found in PATH.

command itself is a builtin function provided by Bash and several other shells. It is defined in POSIX as well, but detailed output and extended behavior may vary depending on the shell.

Basic Syntax

command [-pVv] command [arguments...]

When used without options, it executes the specified command.

command ls -la
command printf '%s\n' 'hello'

In most situations, it appears as if the command was executed directly. The difference becomes apparent when there is a shell function with the same name.

Calling the original command inside a shell function

The following function prints a message before executing ls and then calls the original ls.

ls() {
 printf '%s\n' 'Prints the list.'
 command ls --color=auto "$@"
}

Writing ls "$@" inside the function will call the same function again. command ls bypasses the function lookup, so a Bash built-in command or an external ls in the PATH will be executed.

"$@" passes the arguments given to the function while preserving the original boundaries. Replacing it with $* or unquoted $@ can break arguments that contain spaces or wildcards.

Checking if a command exists using command -v

-v outputs a concise form of how the specified name is interpreted in the current shell. It returns a success status if the command is found and a failure status if not, making it useful for checking existence in scripts.

command -v git
command -v cd
command -v printf

The output can describe not only the executable path but also built-in commands, functions, or aliases. Therefore, the result should not be assumed to be a file path in all cases.

if command -v curl >/dev/null 2>&1; then
 printf '%s\n' 'curl is available.'
else
 printf '%s\n' 'Could not find curl.' >&2
fi

Check detailed information with command -V

-V prints a more human-readable detailed description than -v.

command -V ls
command -V cd
command -V deploy

In automation, sentence formats can vary depending on the shell and locale, so it is recommended not to parse -V output. To check the existence of a script, use the exit status of command -v.

command -p and the default search path

-p uses the default PATH value provided by the shell to locate standard utilities. It can be used when the user's modified PATH is unreliable or when you need to find standard commands.

command -p getconf PATH
command -p ls

The actual value of the default path varies by system. -p does not guarantee a specific absolute path or a program from a specific provider, so in scripts where security is important, you should design the execution environment and trust boundaries separately.

Summary of command options

Option Description
-v Outputs the current shell's interpretation concisely without executing the command.
-V Prints a detailed explanation of how the command is interpreted.
-p Executes a command using the default search path where standard utilities can be found.

Cautions when bypassing aliases

Alias expansion occurs in the shell syntax processing stage before command execution. In interactive Bash, command ls is generally used to execute the actual command instead of the ls alias, but it should not be considered a universal quoting tool that controls both alias definitions and the shell's expansion context.

All candidates for the current name can first be checked with type -a.

type -a ls
command -V ls

If you simply do not want to apply the current alias, you can either enter the name with a backslash or execute the confirmed absolute path.

\ls
/usr/bin/ls

The absolute path may vary depending on the distribution and system structure, so it should be verified before hardcoding.

Comparison of command, builtin, exec, env

Command Main Roles
command name Skips searching for shell functions with the same name and executes a built-in command or a PATH command.
builtin name Explicitly executes a Bash built-in command with the same name.
exec name Replaces the current shell process with a new program. If successful, it does not return to the original shell.
env VAR=value name Execute an external command in an environment with adjusted environment variables.
type name Show how the current Bash interprets a name without executing the command.

For example, if you want to be sure to call the Bash built-in command with the same name inside a function, builtin might be clearer, and if you want to allow external programs as well as functions, command is appropriate.

Principles of using command -v in scripts

Discard output and check the exit status

if! command -v rsync >/dev/null 2>&1; then
 printf '%s\n' 'rsync is required.' >&2
 exit 1
fi

This method does not depend on the output format. To hide even error messages, both standard output and standard error must be redirected.

Additional checks if a regular executable file is required

Even if command -v succeeds, the result may be an alias, function, or built-in command. If an external executable file is required, additionally check whether the result is a path and a regular executable file.

path=$(command -v mytool) || exit 1

if [ -f "$path" ] && [ -x "$path" ]; then
 printf 'Executable file: %s\n' "$path"
fi

Since relative paths or special shell outputs may be returned, design the conditions according to the intended purpose.

Understanding TOCTOU Issues

If you check for the existence of a command and then execute it later, the PATH or files may change in the meantime. This is sufficient for simple user guidance, but in security-sensitive environments, do not trust the check results alone and use controlled paths and permissions.

Exit status

In command execution mode, if the target command cannot be found or an error occurs, it generally returns 127, and if the command is found, it passes the exit status of that command. -v and -V succeed if the name is found and fail if not.

command false
printf 'status=%d\n' "$?"

command does-not-exist
printf 'status=%d\n' "$?"

The exit status follows the meaning of the executed command, so a value other than 0 does not mean that command itself is broken.

Frequently Asked Questions

Does command only execute external commands?

No. In Bash, it skips shell functions and then executes built-in commands or external commands found in the PATH. To find only external files, you need to check the path using tools like type -P.

Is the result of command -v always an absolute path?

No. The result may come from aliases, functions, reserved words, built-in commands, or executable files. Use the exit status to check the existence of a command.

Can it be used in the form of sudo command...?

Since command is a shell built-in, sudo may not be able to execute it directly like a separate executable. Usually, you directly pass the required external command to sudo, or if shell syntax is necessary, you must explicitly run the shell while understanding quoting and security implications.

Why is command -v better than which?

command -v is provided by the shell and reflects the command interpretation of the current shell. which is often an external program, and depending on its implementation, it may not correctly handle aliases or functions.

How can I ignore functions and execute only built-in commands?

In Bash, use builtin name. command name can execute not only built-in commands but also external commands in PATH.

Can the same options be used in other shells?

The core operations of -p, -v, and -V are defined in POSIX, but the output messages and extended features may vary by shell. For automation, specify the target shell and check its documentation.

Summary

command is useful for calling the original command that may be hidden by a shell function and checking if the command can be found in the current shell. Use command name to execute, command -v name to check existence, and command -V name for a human-readable detailed description.

When examining all candidates and types of a command, use type -a together. Other basic commands can be found in the Linux command list and overview.

Refer to official documents

More in This Category
Linux xz Command: Compress a File in xz Format

Linux xz Command: Compress a File in xz Format

Learn how to compress a File in xz Format with the Linux xz command, including practical examples, key options, and important precautions.

Linux curl Command: Make URL Requests and Inspect HTTP Responses

Linux curl Command: Make URL Requests and Inspect HTTP Responses

Learn how to make URL Requests and Inspect HTTP Responses with the Linux curl command, including practical examples, key options, and important precautions.

Linux tmux Command: Detach and Reattach Terminal Sessions

Linux tmux Command: Detach and Reattach Terminal Sessions

Learn how to detach and Reattach Terminal Sessions with the Linux tmux command, including practical examples, key options, and important precautions.

Linux unzip Command: List and Extract ZIP Archives

Linux unzip Command: List and Extract ZIP Archives

Learn how to list and Extract ZIP Archives with the Linux unzip command, including practical examples, key options, and important precautions.

Linux bunzip2 Command: Decompress bzip2 Files

Linux bunzip2 Command: Decompress bzip2 Files

Learn how to decompress bzip2 Files with the Linux bunzip2 command, including practical examples, key options, and important precautions.

Linux xargs Command: Pass Standard Input as Command Arguments

Linux xargs Command: Pass Standard Input as Command Arguments

Learn how to pass Standard Input as Command Arguments with the Linux xargs command, including practical examples, key options, and important precautions.

Linux Tutorial / Manage Files and Directories: Create, Copy, Move, and Delete

Linux Tutorial / Manage Files and Directories: Create, Copy, Move, and Delete

Practice creating, copying, renaming, moving, and deleting files and directories in a dedicated folder while checking each result safely.

Linux man Command: Read Command Manual Pages

Linux man Command: Read Command Manual Pages

Learn how to use the Linux man command to find and read command manuals, sections, options, and related pages, with essential options, practical examples, output interpretation, and common troubleshooting tips.

Linux scp Command: Copy Files over SSH

Linux scp Command: Copy Files over SSH

Learn how to copy Files over SSH with the Linux scp command, including practical examples, key options, and important precautions.

Linux shred Command: Overwrite File Data and Understand Its Limits

Linux shred Command: Overwrite File Data and Understand Its Limits

Learn how Linux shred overwrites file data, which options control its passes and removal, and why SSDs, snapshots, backups, and modern filesystems limit its guarantees.