type Command in Linux
Overview
The type command in Linux is a shell built-in command used to determine the type of command for a given command name. This command allows you to distinguish between shell built-ins, aliases, files on the disk, and keywords, thus it plays a crucial role in understanding and troubleshooting shell scripts.
Syntax of type Command in Linux
The basic syntax of the type command in Linux is as follows:
Where:
- type: The command itself, used to determine the command type.
- [-aftpP]: Options to modify the behavior of the type command.
- [name ...]: The command name for which you want to determine the type.
Options in type Command in Linux
-
-a: This option prints all locations containing an executable named 'name'. This includes aliases and functions, if and only if the '-p' option is not also used.
For example -
Output:
The 'ls' command is an alias and also an executable located at /bin/ls.
-
-t: If the '-t' option is used, 'type' outputs a single word indicating the command type. The possible output words are 'alias', 'keyword', 'function', 'builtin', 'file' or 'not found'.
For example -
Output:
The 'echo' command is a shell builtin.
-
-p: The '-p' option returns either the name of the disk file that would be executed, or nothing if 'type -t name' would not return 'file'.
For example -
Output:
The 'ls' command would execute the disk file located at /bin/ls.
Example Usages
-
Find the type of the 'pwd' command.:
Output:
Explanation: The 'pwd' command is a shell builtin.
-
Use type command to find whether 'ls' is an alias or an executable.:
Output:
Explanation: The 'ls' command is both an alias and an executable located at /bin/ls.
Tips
-
Use type with the '-a' option to get all information about a command.
-
Use 'type -p' to know the path of the executable file for the given command.
Advanced Use Cases of type Command in Linux
-
Find the type of multiple commands at once:
Output:
Explanation: The 'ls' command is an alias and an executable, whereas 'pwd' and 'echo' are shell builtins.
-
Use type to find out whether a command is a function:
Output:
Explanation: The command 'someFunction' is a function.
-
Use type to find out if a command doesn't exist:
Output:
Explanation: The command 'nonExistentCommand' does not exist.
Conclusion
-
The type command in Linux is a powerful tool for understanding and debugging shell scripts.
-
It helps you distinguish between built-in commands, aliases, files on disk, and keywords.
-
You can also use it to find the path of the executable file for a given command.