Search code examples
bashcurlsudo

bash, test if curl exists, and install if it does not


I googled many solutions but did not find one that fully worked. My own rough working is:

[ $(which curl) -eq "" ] || sudo apt install curl

This doesn't quite work, saying "unary operator expected". I've tried = or ==, so I'm not getting how to do a test like this. Is it possible that the empty output from which curl is a $null and I have to test for that?

Alternatively, is there a way to just run sudo apt install curl and then 2>&null at the end to suppress if it's already installed?


Solution

  • which returns a failure if it doesn't find the program, so you can

    which curl &> /dev/null || sudo apt install curl
    

    Note that if someone has a different program in $PATH named curl, the script can still fail later.

    -eq compares numbers, not strings.