Languages
[Edit]
EN

Bash - check if command is supported

8 points
Created by:
Nataniel-Barclay
499

In this short article, we would like to show how to check in Bash script if some command is supported under Linux.

Quick solution:

#!/bin/bash

if ! command -v command_name_here &> /dev/null
then
    echo "command_name_here command is not supported!"
    exit 1
fi

 

Alternative solutions

We are able to use && or || conditions to check command results:

#!/bin/bash

command -v command_name_here &> /dev/null || echo "command_name_here command is not supported!"

We are able to combine all if in one line: 

#!/bin/bash

if ! command -v command_name_here &> /dev/null; then echo "command_name_here command is not supported!"; exit 1; fi
Donate to Dirask
Our content is created by volunteers - like Wikipedia. If you think, the things we do are good, donate us. Thanks!
Join to our subscribers to be up to date with content, news and offers.

Bash

Bash - check if command is supported
Native Advertising
🚀
Get your tech brand or product in front of software developers.
For more information Contact us
Dirask - we help you to
solve coding problems.
Ask question.

❤️💻 🙂

Join