Open gedit ~/.bashrc file and add this script at the end of that (don't forget you have to add this script at the END of file):
NewMail(){
NEWMAIL=$(mailx &)
UNREAD=$(echo $NEWMAIL |grep -o 'messages.*new' | cut -f2 -d" ")
}
NewMail # call NewMail function
if [ -n "$UNREAD" ]; then
PS1="[Hi, alex. you have $UNREAD new mail(s)] $PS1"
fi
We wrote a function whose name is NewMail.
Explanation of function:
The result of first line(NEWMAIL=$(mailx)) according to you information in body of question are:
"/var/mail/alex": 2 messages 2 new [...] # if you have a new unread mail
OR
No mail for alex # if you don't have a new mail
That I stored the above result in a NEWMAIL variable there.
I passed the result into next command in second line with this command echo $NEWMAIL.
Then with this command grep -o 'messages.*new' I get the result just between "message" word and "new" word, which is: messages 2 new.
-o - Print only the matched (non-empty) parts of a matching line, with each such part on a separate output line.
Now we have this messages 2 new result. Then I pass this result to next command cut -f2 -d" ", to getting just number of messages:
-f flag used for fields and -f2 select only second field from "messages 2 new"
-d" " with one space here is used for field delimiter.
Then after running second line, we have the number of new unread mail and that is: 2 here.
In this NewMail # call NewMail function line, we call the NewMail function.
With this if condition, I check if you have new mail. Then the command prompt will change and shows: "[Hi, alex. you have # new mail(s)]" before the prompt. If you have not got any new mail then the command prompt will not change.
if [ -n "$UNREAD" ]; then
PS1="[Hi, alex. you have $UNREAD new mail(s)] $PS1"
fi
-n flag with [ -n "$UNREAD" ] checks if the length of UNREAD is Not zero.
PS1 is defines your command prompt that configure into .bashrc file in your home directory. This is what we open/edit this file. Then I edit that to include unread message count before your command prompt. See:
PS1="[Hi, alex. you have $UNREAD new mail(s)] $PS1"
Here's the screenshot if I have new mails:

And if I have not got any new mail:

That's it. Just copy and paste the script at the end of your ~/.bashrc file.