Bash Shell Find Out If A Command Exists On UNIX / Linux System ($PATH) OR Not

To find out if a command exists on a Unix/Linux system, you can use the which or the type command. Both commands search for the specified command in the directories listed in the system’s $PATH environment variable.

Here’s how to use the which command:

which command_name

Replace command_name with the name of the command you want to check. The which command will search for the specified command in the directories listed in the $PATH environment variable and will output the path to the command if it exists, or nothing if it doesn’t.

Here’s an example:

which ls

Output:

/bin/ls

In this example, which found the ls command in the /bin directory, and output its full path.

Here’s how to use the type command:

type command_name

Replace command_name with the name of the command you want to check. The type command will tell you whether the specified command is an alias, a function, a built-in command, or an external command (i.e., a command located in a file). If the command is an external command, type will output its full path.

Here’s an example:

type cd

Output:

cd is a shell builtin

In this example, type determined that cd is a built-in command.

By using the which and type commands, you can quickly determine if a command exists on a Unix/Linux system and where it is located.

Leave a Comment