Reputation: 444
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:
there is not such a notification in my zsh (maybe I forget something in my .zshrc)
how to change the format of "new mail notification" in bash/zsh
how to execute a certain command
after any of my command is finished in bash/zsh. e.g. when I type ls
and <enter>
, ls
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?
Upvotes: 5
Views: 3196
Reputation: 246744
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'
$ whoami
jackman
$ (exit 3)
[3] $ pwd
/home/jackman
$
Upvotes: 1
Reputation: 241671
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.)
Upvotes: 4
Reputation: 23796
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 $
Upvotes: 1