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

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


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):

MAILPATH  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.)


I don't really know how to go about questions 1 and 2, but one way to execute a certain command after each command finished in the interactive shell prompt (question 3), is to add your code in the prompt variable PS1.

Here is an example with the date command:

$ PS1="\$(date) $ "Fri Jun 21 22:49:00 BRT 2013 $ echo how cool is this?how cool is this?Fri Jun 21 22:49:02 BRT 2013 $


There's also the PROMPT_COMMAND bash variable:

PROMPT_COMMAND

If set, the value is interpreted as a command to execute before the printing of each primary prompt ($PS1).

Suppose you want to know if the previous command exited with a non-zero status:

$ PS1='\$ ' PROMPT_COMMAND='r=$?;(($r != 0)) && printf "[%d] " $r'$ whoamijackman$ (exit 3)[3] $ pwd/home/jackman$