Check if a command exists in Bash
You may need to verify the existence of a command, program, or file using Bash scripting or programming. This article will take a look at several methods to meet this need.
We can use different built-in commands in Bash to check if a command exists. The usage of these commands is demonstrated below.
Use the command -v command to check if a command exists in Bash
The command -v is a built-in function in all POSIX systems and Bash. This function checks whether the command exists and returns a valid path to the command if it exists, or NULL if it does not exist.
For example:
command -v ls
Output:
/bin/ls
Command -v can also be used safely in Bash scripts to check if a command exists with an if condition as shown below.
if ! [ -x "$(command -v npm)" ]; then
echo 'Error: npm is not installed.' >&2
exit 1
fi
The above code will check if npm is installed, i.e. if it exists in the user directory and if it is executable. If npm is not found on Path, the above code will throw an exception and terminate.
The above template can be used to check if a program/command/utility exists by using the name of that program/command/utility.
Use the type command to check if a command exists in Bash
type command is a very useful built-in command which provides information about the entity it is used with. It can be used with commands, files, keywords, shell built-in commands etc.
The usage of the type command is as follows.
type command
Output:
command is a shell builtin
More specifically, for our use case, we can use the type command with the -p option to get the path to a file or executable. Its use is demonstrated below.
type -p npm
Output:
/usr/local/bin/npm
Since npm is installed on our system, type -p
its valid path is returned. It is important to note that if the type command (without any flags) is used with an entity that does not exist, it will throw an error; however, type -p in the same situation will return NULL.
This behavior is demonstrated below.
type yarn
Output:
bash: type: yarn: not found
Since yarn is not installed on our system, an error is returned. However, type -p yarn
no output is returned; the -p flag can be used or omitted depending on how and why you need to check for the existence of the program.
Check if a command exists in Bash using the hash command
The hash command works similarly to the type command. However, it exits successfully if the command is not found.
It also has the added benefit of hashing the queried command, making lookups faster.
The syntax of the command is as follows.
hash -t ls
In this case, the query command is ls. Depending on your system, the output will be similar to the following.
/usr/bin/ls
This is the file location that is run when the command is called. If the command is not found, for example, when the query is similar to: hash -t nothing
, the output will be as follows.
bash: hash: nothing: not found
The output when a command is found is descriptive and offers the added benefit of hashing the command for faster searching next time.
Use the test command to check if a command exists in Bash
test is a built-in shell command that is primarily used for comparison and conditional statements; it can also be used to check if files and directories exist; it is important to note that test only works if a full valid path to the file or directory being checked is provided.
There are many option flags for the test commands related to files and directories, a list of these option flags is given below.
It is important to note that these flags will only return true if the file exists and special conditions are met. You can find the special conditions listed below.
- -b FILE - True if file is a special block file.
- -c FILE - True if file is a character special file.
- -d FILE - True if this is a directory.
- -e FILE - True if it is a file, regardless of type.
- -f FILE - True only if it is a regular file (e.g., not a directory or a device).
- -G FILE - True if file has the same group as the user executing the command.
- -h FILE - True if it is a symbolic link.
- -g FILE - True if the set-group-id (sgid) flag is set.
- -k FILE - True if the sticky flag is set.
- -L FILE - True if it is a symbolic link.
- -O FILE - True if it is owned by the user running the command.
- -p FILE - True if this is a pipe.
- -r FILE - True if readable.
- -S FILE - True if it is a socket.
- -s FILE - True if it has some non-zero size.
- -u FILE - True if the set-user-id (suid) flag is set.
- -w FILE - True if file is writable.
- -x FILE - True if it is executable.
In this example, FILE represents the fully qualified path to the file that the user wants to examine.
The following shows the use of a test to check if a file exists.
test -e etc/random.txt && echo "FILE exists."
The above code statement will check if the file etc/random.txt exists and if the test statement returns true, it will output FILE exists.
For reprinting, please send an email to 1244347461@qq.com for approval. After obtaining the author's consent, kindly include the source as a link.
Related Articles
Sort data based on the second column of a file in Bash
Publish Date:2025/03/22 Views:87 Category:OPERATING SYSTEM
-
This article explains how to sort data based on the second column of a file in bash. Overview of the Sort Command in Bash Sort files using the sort command, which places records in a specific order. By default, the sort command sorts files
Bash Script to Add New User in Linux
Publish Date:2025/03/22 Views:172 Category:OPERATING SYSTEM
-
This short article is about creating a Bash script that automates adding users and assigning passwords for Linux operating system. In Linux operating system, useradd command is used to add new users and provide passwords to them. Bash scrip
Including a script file in another Bash script
Publish Date:2025/03/22 Views:104 Category:OPERATING SYSTEM
-
This article discusses different ways to include a Bash script file into another script file. Including files in Bash scripts Including or reusing scripts in Bash is very simple. The source keyword is similar to that in C/C++ #include . To
eval command in bash script
Publish Date:2025/03/21 Views:88 Category:OPERATING SYSTEM
-
This article is about using strings as commands in Bash scripts. For this purpose, the eval command is used. Eval Command in Bash Scripts In some Bash scripts, you have to create a string using variables or input values (for example)
Exit Bash Script
Publish Date:2025/03/21 Views:99 Category:OPERATING SYSTEM
-
This article provides a brief introduction to Bash scripting and discusses exiting a Bash script when an error occurs. It further discusses the limitations and benefits of Bash scripting. What is Bash Scripting A computer script/program tel
Echo Tab Character in Bash Script
Publish Date:2025/03/21 Views:66 Category:OPERATING SYSTEM
-
This article explains how to echo one or more tab characters when using Bash scripts. Echo Tab Character in Bash Script echo The command is simple: it prints whatever is passed to the terminal. Normally, if you save a variable as: example=
Bash 脚本中的 eval 命令
Publish Date:2023/06/11 Views:392 Category:操作系统
-
本文是关于在 Bash 脚本中使用字符串作为命令的。 为此,使用了 eval 命令。Bash 脚本中的 Eval 命令 在某些 Bash 脚本中,您必须使用变量或输入值(例如)创建一个字符串,并在最后将其作为命
退出 Bash 脚本
Publish Date:2023/06/11 Views:269 Category:操作系统
-
本文简要介绍 Bash 脚本,并讨论在出现错误时退出 Bash 脚本。 它进一步讨论了 Bash 脚本的局限性和好处。什么是 Bash 脚本 计算机脚本/程序告诉计算机做什么和说什么。