user127192 user127192 - 1 month ago 22
Bash Question

Email Automation in Shell Script: Add an input when you run the script

I was able to run the script for email automation using this code:

echo 'This is a test' | mail -s 'Test mail' test@mail.com -- -f test@mail.com

However, I would like to be able to add an additional value when I run the script i.e ./email_test.sh value—the value is a variable that I need to include on the email message body.

Thank you,

Answer

You can do this:

#!/bin/bash

body=$1

echo $body | mail -s 'Test mail' test@mail.com -- -f test@mail.com

then call the script as you requests ./email_test.sh value but don't forget to make it executable using chmod +x email_test.sh

Update: If you need to treat the email as a variable then you need to add it as email=$2 just like we did with body=$1 but the email will be the second argument in this case and same goes for the subject.