Linux Type Command

Introduction

The type command is used to display command type information. It will demonstrate how a command would be interpreted if typed directly into the command prompt.

In this tutorial, you will understand type command in Linux. We will also address a few FAQs on Linux Type Command.

How to Use the type Command

type is a shell that comes standard with Bash and other shells such as Zsh and Ksh. Its behavior may vary slightly from one shell to another. We'll look at the built-in version of type in Bash.

The type command has the following syntax:

type [OPTIONS] FILE_NAME...

For instance, to determine the type of the wc command, type the following:

type wc

You will get an output like below:

Output

wc is /usr/bin/wc

You can also use the type command with more than one argument:

type sleep head

Information regarding both sleep and head instructions will be included in the output:

Output

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

Command Types

The -t option instructs type to output a single word that describes the command's type, which can be any of the following:

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

Listed below are a few examples:

1) Alias

type -t grep

grep is aliased to grep --color=auto on our system.

Output

alias

2) Function

type -t rvm

rvm is a function that allows you to install, manage, and work with multiple Ruby environments:

Output

function

3) Builtin

type -t echo

echo is a shell that comes standard with Bash and other shells such as Zsh and Ksh:

Output

builtin

4) File

type -t cut

cut is a program that can be run on your computer:

Output

builtin

5) Keyword

type -t for

In Bash, the term for is a reserved word:

Output

keyword

Display all locations that contain the command

Use the -a option to print all matches:

type -a pwd

pwd is a shell built-in, however it is also available as a standalone /bin/pwd program, as shown in the output:

Output

pwd is a shell builtin
pwd is /bin/pwd

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

Other type command options

Only if the command is an executable file on the disc will the -p option compel type to return the path to the command:

Because the pwd command is a shell built-in, the following command will not provide any output.

type -p pwd

The uppercase -P option, unlike -p, instructs type to look in the PATH for an executable file on the disc even if the command is not a file.

type -P pwd
Output

pwd is /bin/pwd

type will not look up for shell functions when the -f option is given, as it does with the command built-in.

FAQs on Linux Type Command

How do I use the type command? 

Simply type type followed by the command you want to check. For example, type ls will display how the ls command is interpreted by the shell.

Can the type command show if a command is an alias? 

Yes, the type command will indicate if a command is an alias by displaying alias followed by the original command and its definition.

How does the type command identify shell built-in commands? 

The type command recognizes shell built-in commands by displaying shell built-in next to the command name in the output.

Can I use the type command to display function definitions? 

Yes, the type command will show function definitions by displaying function followed by the function name and its definition.

What does the type command display for external commands? 

For external commands, the type command will show the command's location or path in the filesystem.

How can I check the type of all commands available in Linux? 

The type command supports wildcards, so you can use type * to display the types of all available commands in your shell environment.

Can I use the type command to verify if a command exists before running it? 

Yes, you can use the type command in combination with conditional statements in shell scripts to check if a command exists before executing it.

Conclusion

When you use the type command on the command line, it will display how a given command will be parsed.

If you have any queries, please leave a comment below and we’ll be happy to respond to them.