Is there a way to detect if an external command exists (i.e. wget, svn)?

More specifically today, i was trying to run one of my scripts i wrote and the person didn't have wget or svn installed.

The script just downloads a file extracts it or uses svn to export the trunk.

  • wget and svn are not functions, they're external commands (also called utilities, which includes built-in utilities). Functions are defined by shell code and live inside the current shell instance. External commands are stored in executable files and can be implemented in any language. – Gilles 'SO- stop being evil' Sep 1 '11 at 23:53
  • Thank you. I wasn't sure about the terminology there; still have a lot to learn. – chrisjlee Sep 2 '11 at 1:29
  • If you are using Ubuntu or Debian you may be interested in the command-not-found package. – Faheem Mitha Sep 4 '11 at 1:10

In Bash the type shell built-in gives information about the executable things: aliases, functions, executables. See help type for details.

# just check for existence
type -t 'yourfunction' > /dev/null || echo 'error: yourfunction not found'

# explicitly check for given type
[[ "$( type -t 'yourfunction' )" != 'function' ]] && \
    echo 'error: yourfunction not found or is not a function'

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.