Type Command In Linux

superior_hosting_service

How to use Type Command in Linux

Type-command-In_Linux-.png

The type command is used to display information about the command type. It will show you how a given command would be interpreted if typed on the command line.

In this article, we will explain how to use the Linux type command.

How to Use the type Command

type is a shell builtin in Bash and other shells like Zsh and Ksh. Its behavior may be slightly different from shell to shell. We will cover the Bash builtin version of type.

The syntax for the type command is as follows:

$ type [OPTIONS] FILE_NAME...

For example, to find the type of the wc command , you would type the following:

$ type wc

The output will be something like this:

wc is /usr/bin/wc

You can also provide more than one arguments to the type command:

$ type sleep head

The output will include information about both sleep and head commands:

OUTPUT
sleep is /bin/sleep
head is /usr/bin/head

Command Types

The option -t tells type to print a single word describing the type of the command which can be one of the following:

  • alias (shell alias)
  • function (shell function)
  • builtin (shell builtin)
  • file (disk file)
  • keyword (shell reserved word)

Here are a few examples:

Alias

$ type -t grep

In my system grep is aliased to grep --color=auto:

OUTPUT
alias

Function

$ type -t rvm

rvm is a tool (function) for installing, managing, and working with multiple Ruby environments:

OUTPUT
function

Builtin

$ type -t echo

echo is a shell builtin in Bash and other shells like Zsh and Ksh:

OUTPUT
builtin

File

$ type -t cut

cut is an executable file :

OUTPUT
builtin

Keyword

$ type -t for

for is a reserved word in Bash:

OUTPUT
keyword

Display all locations that contain the command

To print all matches, use the -a option:

$ type -a pwd

The output will show you that pwd is a shell builtin but it is also available as a standalone /bin/pwd executable:

OUTPUT
pwd is a shell builtin
pwd is /bin/pwd

When -a option is used, the type command will include aliases and functions, only if the -p option is not used.

Other type command options

The -p option will force type to return the path to the command only if the command is an executable file on the disk:

For example, the following command will not display any output because the pwd command is a shell builtin.

$ type -p pwd

Unlike -p, the uppercase -P option tells type to search the PATH for an executable file on the disk even if the command is not file.

$ type -P pwd
OUTPUT
pwd is /bin/pwd

When the -f option is used, type will not look up for shell functions, as with the command builtin.

Conclusion

The type command will show you how a specific command will be interpreted if used on the command line.

If you have any questions or feedback, please leave a comment below.