Ubuntu – Local mail is (almost) never checked in gnome-terminal, how can I change that


Context : I know a "console" login (with "Ctrl-Alt-Fx") provides a local mail check (of /var/mail/$USER), but a Ubuntu user is not expected to use this for daily use. Instead it is advised to use gnome-terminal for command-line interaction.

What I expect :
When I start gnome-terminal I expect the bash shell to check local mail and reports accordingly "You have new mail" if there is new mail. But it does not work.

What happens instead :
gnome-terminal invokes bash with $MAIL (and $MAILCHECK) variables not set. That's why the user is never informed about new local mail.

What I tried :
I put in ~/.bashrc :

export MAIL=/var/mail/$USER
export MAILCHECK=60

And… it does not really work.
It only works if I receive a new mail while gnome-terminal is running : in this case I will have "You have new mail" at next command line prompt.
If I receive new mail while gnome-terminal is not running, and then I launch gnome-terminal : no notification at all.

Any idea ? (for the record I use Ubuntu 19.10)

Best Answer

  • The short answer is that virtual terminals don't have control over OS login operations. In terms of populating 'the right things' for bash (most shells) to check for mail, the program that actually does this work is login(1) -which is outside the shell configuration.

    There is an elegant answer about how login(1) plays a part in all of this here: bash - Why is MAIL unset in graphical environments? - Unix & Linux Stack Exchange

    With that said, I use the following function to display system mail notifications, if system mail exists, based on a maximum interval. You will have to figure out the prompt hook but this should point you in the right direction. Hope this helps:

    Should be used with 'login shell' mode on terminals...

    # add to your ~/.profile or equivelant
        local pwd='~'
        [ "$PWD" != "$HOME" ] && pwd=${PWD/#$HOME\//\~\/}
        printf "\033]0;%s@%s:%s\033\\%s" "${USER}" "${HOSTNAME%%.*}" "${pwd}"
        # if [ ! "$SSH_CONNECTION" ]; then
            if [[ "$MAIL_CHECK_TIME" -eq "0" || "$MAIL_ELAPSED_SECONDS" -gt "$MAIL_SECONDS_DIFF" ]]; then
                local MAILX="$(mailx 2>/dev/null &)"
                local COUNT=$(echo "$MAILX" | wc -l)
                local COUNT=$((COUNT-3))
                local MESSAGE_TEXT="message"
                if [ "$COUNT" -gt "0" ]; then
                    if [ "$COUNT" -gt "1" ];then
                    printf "You have $COUNT mail $MESSAGE_TEXT.\n \033]0;%s@%s:%s\033\\%s" "${USER}" "${HOSTNAME%%.*}" "${pwd}"
        # fi
        # echo "seconds: $SECONDS"
        # echo "check: $MAIL_CHECK_TIME"
        # echo "elapsed: $MAIL_ELAPSED_SECONDS"
    # uses mx  linux /etc/profile.d prompt hook