Bash: Run command if exists, else echo blank

Useful if you want to only output something if a command exists.

if $(type YOUR_COMMAND &> /dev/null); then RUN_YOUR_COMMAND; else echo ""; fi

For example, I use this on my Proxmox hypervisors with ZFS to get status. Not all my hypervisors have zfs arrays. Makes it easy to blanket ssh to all hypervisors and get zfs status if it exists. For a custom status page I wrote.

if $(type zpool &> /dev/null); then zpool list; else echo ""; fi


Leave a Reply

Your email address will not be published. Required fields are marked *