type

shell builtinLinux/Unix
The type command is one of the most frequently used commands in Linux/Unix-like operating systems. type Display information about command type

Quick Reference

Command Name:

type

Category:

shell builtin

Platform:

Linux/Unix

Basic Usage:

type [options] [arguments]

Common Use Cases

    Syntax

    type [-afptP] name [name ...]

    Options

    Option Description
    -a Display all locations containing an executable named NAME
    -f Suppress shell function lookup
    -p Display the name of the disk file that would be executed, or nothing if 'type -t NAME' would not return 'file'
    -P Force a PATH search for each NAME, even if it is an alias, builtin, or function, and returns the name of the disk file that would be executed
    -t Output a single word: 'alias', 'keyword', 'function', 'builtin', 'file' or nothing if TYPE is not found

    Examples

    How to Use These Examples

    The examples below show common ways to use the type command. Try them in your terminal to see the results. You can copy any example by clicking on the code block.

    # Basic Examples Basic
    # Check the type of a common command type ls
    # Check if a command is a shell builtin type cd
    # Check if a command exists type nonexistentcommand
    # Advanced Examples Advanced
    # Get all locations of a command in PATH type -a python
    # Get the file path of a command type -p grep # Show the command as a string that would be executed type -t bash # Check the type of multiple commands at once type cd ls grep awk # Determine the type of a shell function function my_func() { echo "Hello World"; } type my_func # Find the location of a command that might be aliased type -P ls # Check if a name is an alias alias ll='ls -la' type ll # Get detailed information about a command type -a bash # Check if a command is a keyword type if # Display the command definition for a shell function function greet() { echo "Hello, $1!"; } type greet # Check command type in a conditional statement if type -t python3 >/dev/null; then echo "Python 3 is installed" else echo "Python 3 is not installed" fi

    Try It Yourself

    Practice makes perfect! The best way to learn is by trying these examples on your own system with real files.

    Understanding Syntax

    Pay attention to the syntax coloring: commands, options, and file paths are highlighted differently.

    Notes

    The `type` command is a shell builtin in Unix-like operating systems that helps determine how a given name would be interpreted if used as a command. It's particularly useful for troubleshooting, scripting, and understanding how the shell resolves command names. When working in a shell environment like Bash, there are several types of commands available: 1. **Aliases**: User-defined shortcuts for other commands 2. **Shell Functions**: Named blocks of commands defined within the shell 3. **Shell Builtins**: Commands that are built into the shell itself 4. **Keywords**: Reserved words that have special meaning to the shell 5. **External Commands**: Executable files located in the directories listed in the PATH environment variable The `type` command helps disambiguate between these different command types, which is particularly important because they have different precedence in the shell's command lookup order. For example, if you have an alias named `ls`, a shell function named `ls`, and the standard `/bin/ls` executable, the shell would typically use the alias first. The `type` command lets you see this lookup order and understand which version of a command would be executed. This command is especially valuable in shell scripting where you might need to: - Check if a command exists before trying to use it - Determine if a command is a builtin or an external program - Find the actual executable path for a command - Verify that a name isn't already in use before defining a function or alias The `-a` option is particularly useful as it shows all locations where a command exists, allowing you to see every matching command in PATH, as well as any builtins, functions, or aliases with the same name. The `-t` option outputs a single word indicating the type, which is useful in scripts for conditional logic based on command type. The `-p` and `-P` options focus on finding the executable file path, which can be helpful when you specifically need to know which binary would be executed. While `type` is similar to the `which` command, it's more comprehensive because it recognizes shell builtins, functions, and aliases, whereas `which` only locates executables in PATH. Overall, the `type` command is an essential tool for shell users and script writers who need to understand and validate how commands are resolved in the shell environment.

    Related Commands

    These commands are frequently used alongside type or serve similar purposes:

    Use Cases

    Learn By Doing

    The best way to learn Linux commands is by practicing. Try out these examples in your terminal to build muscle memory and understand how the type command works in different scenarios.

    $ type
    View All Commands