linuxbash

How to check dependency in bash script


I want to check whether nodejs is installed on the system or not. I am getting this error:

Error : command not found.

How can i fix it?

#!/bin/bash

if [ nodejs -v ]; then
echo "nodejs found"
else
echo "nodejs not found"
fi

Solution

  • You can use the command bash builtin:

    if command -v nodejs >/dev/null 2>&1 ; then
        echo "nodejs found"
        echo "version: $(nodejs -v)"
    else
        echo "nodejs not found"
    fi