'command' Command in Linux



The 'command' is a Linux command that allows you to run other commands with specific conditions. You can control how and when a command executes using this power tool. It is like a command for running commands on your Linux system.

Table of Contents

Here is a comprehensive guide to the options available with the 'command' command −

Syntax of 'command' Command

The basic syntax to use the 'command' command on Linux is provided below −

command [option] [argument]

Here, option specifies additional behavior for the command, while argument represents the command you want to execute.

'command' Command Options

With 'command' command, you can use different options, these are discussed in the table given below −

Options Description
-p Use the system's default search path for the command. This is useful when you want to run a command without specifying its full path.
-V Display version information for the 'command' command itself. This shows details about the version of the command utility.
-v Enable verbose mode. When used with a specific command, it displays additional information or debugging output related to that command.

Examples of 'command' Command in Linux

The basic function of the 'command' command on Linux is to allow you to run another command with specific conditions. Let's consider the following example −

command -p pwd

Here,

  • command is the base command that allows you to run another command.
  • -p is the option that specifies using the system's default search path for the command.
  • pwd is the argument that represents the command you want to check.
'command' Command in Linux 1

If you use -v option with the 'command' command, it will print a description of the specified command. For example, the following command will print the description of the pwd command −

command -V pwd
'command' Command in Linux 2

If the command is an alias or function, it returns the alias/function name without revealing its contents. For example −

command -v ls
'command' Command in Linux 3

You can also use the 'command' command with -V option to print a more detailed description of the command. For example, if you use the below-given command, it will print the information about the pwd command.

command -V pwd
'command' Command in Linux 4

If the -V option is used with the comm command followed by the ls argument, it reveals that ls is aliased to 'ls --color=auto'.

command -V ls
'command' Command in Linux 5

Advantages and Disadvantages of 'command' Command

The 'command' command in Linux offers a useful advantage: it allows you to bypass shell functions. This is pretty useful especially when you want to use a utility instead of a similarly named shell function. However, it is important to note that the 'command' command will not work with shell aliases. If you have an alias having the same name as a utility, then using command will not bypass the alias; you should keep this in mind while working with aliases.

Alternatives of 'command' Command

There are some alternatives of 'command' that you can use to perform similar kinds of actions. Understanding these commands will broaden your command-line skills and you can use them whenever you need.

type Command

The type is a useful Linux command that helps you determine how a command is interpreted by the shell. It provides you with the information about whether the command is a shell function, an alias, executable file or a built-in command. For example −

type ls

which Command

The which is a commonly used Linux command that allows you to locate the executable file associated with a given command. With which command, you will be able to know about the path to the command's binary. This also helps to ensure that a certain command is installed on your system. For example −

which ls

whereis Command

You can use the whereis command on Linux to find information about the location of a command, its manual pages or source files. With this command, you can search for both binary and source files on your Linux system. For example −

whereis cat

Conclusion

'command' is a powerful Linux command that is used to run other commands under specific conditions. In this tutorial, we covered the syntax for 'command' command, different options you can use with it and examples to help you grasp the basics of the command. We also explained the advantages and disadvantages of 'command' along with some alternatives you can also use.

Advertisements