From 31289a98f1dfa760226ae77a064a18d2bd015057 Mon Sep 17 00:00:00 2001 From: Dylan Araps Date: Fri, 15 Jun 2018 16:10:23 +1000 Subject: [PATCH] Added another entry --- README.md | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/README.md b/README.md index 172fc92..db7aceb 100644 --- a/README.md +++ b/README.md @@ -99,6 +99,7 @@ scripts and not full blown utilities. * [Get the current working directory.](#get-the-current-working-directory) * [Get the number of seconds the script has been running.](#get-the-number-of-seconds-the-script-has-been-running) * [Other](#other) + * [Check if a program is in the user's PATH.](#check-if-a-program-is-in-the-users-path) * [Get the current date using `strftime`.](#get-the-current-date-using-strftime) * [Bypass shell aliases.](#bypass-shell-aliases) * [Bypass shell functions.](#bypass-shell-functions) @@ -1113,6 +1114,29 @@ This is an alternative to the `pwd` built-in. # Other +## Check if a program is in the user's PATH. + +```shell +# Bare. +type -p executable_name &>/dev/null + +# As a test. +if type -p executable_name &>/dev/null; then + # Program is in PATH. +fi + +# Inverse. +if ! type -p executable_name &>/dev/null; then + # Program is not in PATH. +fi + +# Example (Exit early if program isn't installed). +if ! type -p convert &>/dev/null; then + printf '%s\n' "error: convert isn't installed, exiting..." + exit 1 +fi +``` + ## Get the current date using `strftime`. Bash’s `printf` has a built-in method of getting the date which we can use