linux,

How to check if command exist in a Bash/Shell script

Aug 15, 2022 · 1 min read · Post a comment

Creating Bash or Shell script for validating whether some command exists or not, can be necessary in some cases. Here I’m going to give you a quick example of how can you achieve it.

Prerequisites

  • Linux
  • sudo privileges

Solution

I’ve created a bash script that checks whether the telnet command exists or not. There are two cases:

  1. If doesn’t exist proceed with the installation.
  2. If exist, print that.
#!/usr/bin/env bash

if ! command -v telnet &> /dev/null
then
    echo "Telnet could not be found. Installing it ..."
  #  installation part
else
    echo "Telnet is installed"
fi

Conclusion

Feel free to leave a comment below and if you find this tutorial useful, follow our official channel on Telegram.