The bash mail: command not found error can be triggered usually in fresh or minimal installations of any Linux operating system. The reason for this is the operating system vendors tend to include the bare minimum programs, tools, and utilities to make the system operational.
The side-effect of this is that you are left without the option to send mail from the command line should you require to do it directly or through a bash script.
There are other programs that can be used to send emails in Linux however today we are focusing on the mail command utility.
Install Bash Mail Command
For those with Red hat (RHEL) based systems, this command is installed from mailx rpm packages while for those on Debian-based systems the command will be installed from the mailutils package.
Install mail command on CentOS/Redhat:
$ sudo yum install mailx
Install mail command on Ubuntu/Debian
$ sudo apt-get install mailutils
Send a Test Email to Confirm
After installing mail command packages on your system, you may want to send a test email using the below example.
$ echo "Message Body" | mail -s "Message Subject" [email protected]
You can specify the sender using the command below:
$ echo "Message Body" | mail -s "Message Subject" [email protected] -a From:[email protected]
You can also load the message body from a file using the command below:
$ mail -s "Message Subject" [email protected] -a From:[email protected] < /path/to/filename
In most cases, this is sufficient for simple diagnostics and alerts mailing you can use from your bash scripts. To handle more business kind use cases you may need other programs such as Postfix, Sendmail, Dovecot, fail2ban, and other systems to handle sending, receiving, and spam management.
Anyway, that is how you install and use the bash mail command in Linux operating systems such as CentOS, Redhat, Ubuntu, and Debian among others.