wenlibin02 wenlibin02 - 1 month ago 4x
Linux Question

How to give a (new mail) notification in bash/zsh automatically?

Well, the title of my question is a little vague. Now let me explain that clearly.

We know there is a "MAILCHECK" in bash, every few minutes bash will check the mailbox and give you a message if you have new mails. Note that you don't need a command for this notification. Bash print a message automatically anytime if there are new mails.

Here, I have few questions:

  1. there is not such a notification in my zsh (maybe I forget something in my .zshrc)

  2. how to change the format of "new mail notification" in bash/zsh

  3. how to execute
    a certain command
    after any of my command is finished in bash/zsh. e.g. when I type
    will be executed, and then
    the certain command
    will be executed.
    If I can do this, the automatic notification is done!

Is that clear? Any suggestion?


1. Mail notification in zsh:

I think it's just like bash; mail notification will take place if the shell knows where to look for mail and if the MAILCHECK parameter is set to a non-negative integer.

2. Changing the mail notification message.

(from man bash):

  A colon-separated list of file names to be checked for mail.  The message to be
  printed when mail arrives in a particular file may be specified by separating
  the file name from the message with a '?'.  When used in the text of the
  message, $_ expands to the name of the current mailfile.  Example:
    MAILPATH='/var/mail/bfox?"You have mail":~/shell-mail?"$_ has mail!"'
  Bash  supplies  a default value for this variable, but the location of the user
  mail files that it uses is system dependent (e.g., /var/mail/$USER).

I think zsh is roughly the same, aside from also exposing mailpath as the array version of MAILPATH.

3. Running arbitrary commands:

In bash, the value of PS1 is printed as a command prompt. Unless the promptvars options is unset (it is set by default), the string undergoes parameter expansion, command substitution, arithmetic expansion and quote removal before being used. The second of those means that you can execute arbitrary shell commands as part of the command prompt.

zsh has the same feature, controlled by the shell option promptsubst (or PROMPT_SUBST, as the manpage says). Unlike bash, the shell options is unset by default. Also, you might find that you are unable to change the value of PS1 (if your distro uses prompt themes), because the prompt theme resets PS1 before every command prompt.

It turns out that zsh has a different mechanism for running shell functions before the prompt is printed (or in other circumstances; I'm just going to focus on this one case). There is an array parameter called precmd_functions whose values are the names of functions which will be run before every prompt. (The prompt theme systems uses this mechanism to reset PS1 before it is printed.)