Check for existence of wget/curl

Linux has a which command which will check for the existence of an executable on your path:

pax> which ls ; echo $?
/bin/ls
0

pax> which no_such_executable ; echo $?
1

As you can see, it sets the return code $? to easily tell if the executable was found.


wget http://download/url/file 2>/dev/null || curl -O  http://download/url/file

One can also use command or type or hash to check if wget/curl exists or not. Another thread here - "Check if a program exists from a Bash script" answers very nicely what to use in a bash script to check if a program exists.

I would do this -

if [ ! -x /usr/bin/wget ] ; then
    # some extra check if wget is not installed at the usual place                                                                           
    command -v wget >/dev/null 2>&1 || { echo >&2 "Please install wget or set it in your path. Aborting."; exit 1; }
fi

Tags:

Linux

Shell