Bash script to send email from your terminal

Endrit Qerreti

Endrit Qerreti

In this tutorial, we are going to write a bash script that will send an email from your terminal on Linux. The script is simple, it asks you enter 3 inputs, message, subject and the email address where you want to send this email to.

Script also checks if the package mailutils is installed on your system or not, and if this package is not found on your system then the script will install it for you.

mailutils is the package that we are using to send the email, and it is required to be installed on your system before you use our script to send the emails.

Once script checks for the requirements you have to press enter to continue sending your emails.

Step 1 - Create

I chose as the name for the script, so to create we can use the touch command


Step 2 - Bash script

Copy the script below into, and press ctrl +x to save the code


#Version : 1.0
#Written by: Endrit Qerreti
#Copyright (c) Endrit Qerreti 2023

if_mailutils="$(dpkg -l | grep mailutils)"

if [[ -z "$if_mailutils" ]];
echo  -e "mailutils doesn't seem to be installed on your system\nPress enter to install it\n"
read required
install_mailutils="$(sudo apt install mailutils -y)"
echo -e "mailutils is installed, you are good to go ahead and send your email,\npress enter to continue"
read continue

message="Write the message:"
echo -e "\n"$message"\n"
read message

subject="Set the subject:"
echo -e "\n"$subject"\n"
read subject

email="Set the email address you want to send the email to:"
echo -e "\n"$email"\n"
read email

echo -e "\nThe email is being sent to $email"

send_email=$(echo "$message" | mail -s "$subject"  $email)


Note: This script is built and tested on Debian 11, the script itself cannot send an email using the mail -s command if you don't have postfix configured on your machine.
However, postfix it's not the only solution to achieve this, you can also use ssmtp instead of postfix, follow our tutorial here.

Get our premium bash collection

And you should be good to go and make executable

Step 3 - Make executable

To make executable by only the user account that you are using, run

chmod u+x

If you wan to make it executable by everyone then you can use

chmod +x

Assuming you have multiple users on your system and you want all of them to be able to run the script, then use chmod +x

Step 4 - Launch

Once you have saved the bash script into, and given the right permission for executing, all you have to do now is to launch it

Open your terminal and run


Step 5 - Send your email

When you launch, script will perform a check to see if mailutils package is installed on your system.  

If mailutils is installed on your system you should see the message " mailutils is installed, you are good to go ahead and send your email, press enter to continue" and you will need to press enter to continue using the script

If mailutils is not installed on your system, script will prompt with "mailutils doesn't seem to be installed on your system, press enter to install it" Then simply press enter to install mailutils.

Next, you need to enter :

1) Message

2) Subject

3) Email address to send the email

And the script will display a message that the email was sent.


In this tutorial, we wrote a simple bash script that you can use to send emails directly from your terminal to any email address.  You don't have to type or remember a long command in order to send an email using terminal, makes this task easier for you.