Home  >  Article  >  Computer Tutorials  >  Linux type command

Linux type command

WBOY
WBOYforward
2024-03-20 17:06:06718browse

Linux type command

In this guide, we will learn more about the "type" command in Linux.

Prerequisite:

To perform the steps demonstrated in this guide, you will need the following components:

  • Correctly configured Linux system. Check out how to create a Linux VM for testing and learning purposes.
  • Basic understanding of command line interface

Type command in Linux

Unlike other Linux-specific commands (such as: ls, chmod, shut, vi, grep, pwd, etc.), the "type" command is a built-in Bash function that displays information about the command type provided as a parameter.

$type

In addition to Bash, other shells (Zsh, Ksh, etc.) also come with "type" commands that they execute.

Basic usage

The command structure of type

is as follows:

$type

The simplest usage is to provide the command as a parameter.

$type, where

The "type" command can also handle multiple parameters at the same time.

$type type which cd ls chmod sudo sleep shoff

Command type

If we are only interested in the type of command, then use the "-t" flag to get only the command type.

$type—t echo

$type—t type

$type—t while

$type—t grep

The output is one of the following command types:

  • Alias: shell alias.
  • function: built-in shell function.
  • Builtin: A built-in shell command.
  • file: Disk file.
  • Keywords: reserved words used specific to shell functions.

All locations

Various commands on Linux exist both as stand-alone executable files and as built-in shell functions. We can use the "type" command to determine whether a command has these two characteristics.

Check out the examples below:

$TYPE-AEcho

Here, as the output shows, the "echo" command is a built-in shell function and an executable file (located in "/usr/bin/echo").

Other options

In addition to the options mentioned above, the "type" command also comes with some additional options.

"—p" flag

Run the following command:

$type—p echo

$type-pClose

Here, if the given parameter is a built-in shell, the "type" command will not display any output. Otherwise, the output will be the location of the command's executable file.

"P" flag

Check out the examples below:

$P type echo power supply

$type—P,while

Here, the "type" command will search all PATH locations for the given parameters and return the location of the matching executable file.

In this example, the "echo" and "pwd" commands have dedicated binaries. "if" and "while" are both shell keywords and have no dedicated binary code. The output is empty.

Exit code

After performing its task, the "type" command leaves an exit code. Using the exit code, we can determine whether the task was successful.

  • 0: The command ran successfully without any errors.
  • 1: The command faced an error.

The following command demonstrates the exit code:

$type

$echo $?

$type asdfg

$echo $?

in conclusion

In this guide, we demonstrate how to use the "type" command in Linux. It is a shell built-in command that describes the nature of the command.

Interested in learning more about other Linux commands? Check out the Linux commands subcategory. For more Bash-related guides, check out Programming with Bash.

Happy computing!

The above is the detailed content of Linux type command. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:mryunwei.com. If there is any infringement, please contact admin@php.cn delete