how to get name of the current command in zsh? (to email yourself shell output in zsh with a good Subject line)

Viewed 191

In tcsh, the most "unscriptable" shell, I often use the following code to email myself output of any command, with the subject line conveniently set exactly to what the command was:

~/script.sh | & mail -s "`history 1| cut -f3-`" user@example.org

Here's the example session to show that the output I get is exactly as the input command:

tcsh% printf "%s\n" "`history 1| cut -f3-`"
printf "%s\n" "`history 1| cut -f3-`"
tcsh%

Note that it works perfectly with the long-term history storage, too, and can be modified without having to be written again.


I've tried using the following in zsh, but, it's one command behind, so, does not produce the required output, plus it looks pretty ugly with the extra -d and tr being required:

\history -1 | tr -s ' ' | cut -d\  -f3-

I've also tried !# in zsh, but it results in broken history (stuff gets duplicated), and doesn't even work even work properly by itself, either:

zsh% printf "%s\n" "!#"
dquote>

Is there any way in zsh to get the whole command line that's being executed such that it could be used as the subject line for the email?

0 Answers
Related