How to Use the Linux Command type and Its 4 Options

The Linux Command type is useful for identifying the nature of a command entered by the user. In this article, we will explain the usage, options, and precautions of the type command in detail.

What is the Linux Command type?

The type command in Linux is a tool that tells you the nature of a command entered in the shell. For example, it informs you whether the entered command is a built-in command, an external command, an alias, or a function.

Basic Usage

The most basic usage of the type command is as follows, where [command] is the command you want to check:

type [command]
ShellScript

Built-in Commands

If the command is a built-in command, it will inform you that it is a shell built-in command.

Figure 1. Linux Command type: Checking a built-in command
Figure 1. Linux Command type: Checking a built-in command

External Commands

If the command is an external command, it will clearly indicate the path where the command file is located.

Figure 2. Linux Command type: Checking an external command
Figure 2. Linux Command type: Checking an external command

Aliases

If the command is an alias, it will show you the alias value.

Figure 3. Linux Command type: Checking an alias
Figure 3. Linux Command type: Checking an alias

Options for the type Command

The type command provides more detailed information through various options. The main options are as follows:

The -a Option

The -a option outputs all locations of the specified command. This is useful when there are multiple commands with the same name in different locations.

type -a [command]
ShellScript

The following example shows the result of using the -a option to output all locations of the nano command:

Figure 4. Linux Command type: Outputting all locations of a command
Figure 4. Linux Command type: Outputting all locations of a command

The -t Option

The -t option only outputs the type of the command. It simply tells you whether the command is a built-in command, an external command, or an alias.

type -t [command]
ShellScript

In the image below, you can see that cd is a built-in command, nano is an external command, and ll is an alias.

Figure 5. Linux Command type: Outputting the type of a command
Figure 5. Linux Command type: Outputting the type of a command

The -p Option

The -p option outputs only the absolute path of the command. If it cannot find the path, it returns an empty value.

type -p [command]
ShellScript

As shown below, since cd and ll paths cannot be found, nothing is output. However, for nano, the absolute path of the command is displayed.

Figure 6. Linux Command type: Outputting the absolute path of a command
Figure 6. Linux Command type: Outputting the absolute path of a command

The -P Option

The -P option searches for the absolute path of the command in the directories listed in the PATH environment variable. It performs a more comprehensive search compared to the -p option.

type -P [command]
ShellScript

The following example shows that ls has both an alias and an external command. Using the -p option results in no output, but the -P option searches the directories in the PATH environment variable and displays the absolute path of the command.

Figure 7. Linux Command type: Outputting the absolute path of a command from the PATH environment variable
Figure 7. Linux Command type: Outputting the absolute path of a command from the PATH environment variable

Useful Applications of the type Command

The type command can be useful in the following situations:

  1. Resolving Command Conflicts: When there are multiple commands with the same name in different locations, you can check which command will be executed.
  2. Script Debugging: When writing shell scripts, you can verify whether the commands used are interpreted correctly.
  3. Alias Management: When you set aliases for frequently used commands, you can distinguish between the actual command and the alias.

Precautions

When using the type command, there are a few precautions:

  1. PATH Environment Variable: The type command searches for commands based on the directories listed in the PATH environment variable. Ensure that the PATH is set correctly.
  2. Distinguishing Built-in and External Commands: Built-in commands are integrated into the shell and do not have paths, whereas external commands are located in the file system. The -P or -p options do not output anything for aliases or built-in commands, so do not be alarmed if nothing is displayed.

Summary

The Linux Command type is a very useful tool for identifying and managing the types of commands. By using various options, you can obtain detailed information about commands, which is very helpful for system management and script writing. Make sure to understand the usage and precautions explained above to use it efficiently.

As you learn how to use Linux effectively, the type command is one of the essential tools. By using it, you can clearly understand the type and location of commands and manage the system more systematically.

References

Leave a Comment