Hi Chet & everyone, The goal is to notify the user if a long-running command (e.g. a compilation) finishes, and the notification should mention the given command.
$BASH_COMMAND seems to contain the command that's about to be executed, whereas what we'd need is the command that just terminated. $BASH_COMMAND doesn't seem to contain that e.g. within PROMPT_COMMAND's execution. To understand the complete situation, let's start with this demo example: PROMPT_COMMAND='notify-send "Program $(history 1 | cut -c8-) completed"' (Replace notify-send with xmessage if it's not available for you.) It's a nice proof of concept to demonstrate what we'd like to achieve, but suffers from several issues. Some of them will be addressed in gnome-terminal (e.g. instead of notify-send it'll emit an escape sequence which will only make it to a desktop notification if the terminal tab is not the active one -- these are irrelevant here). Some others are issues that should be addressed in bash: - "history 1" is not a reliable way of figuring out the previous foreground task (whose termination caused bash to display the prompt). There are 2 issues I'm aware of: It incorrectly reports "fg" instead of the command line if job control was used; and it reports the preceding command if the current one began with a space and ignorespace is set. - It is executed every time the prompt is displayed, e.g. immediately upon shell startup, and whenever you just press a lone Enter or suspend the fg job. (Now, as said, gnome-terminal will filter out such events if the tab is not the foreground one, but over a slow ssh connection it might easily happen that you press Enter and switch to another tab before the prompt gets printed again. So we should never emit faulty notifications and then expect gnome-terminal to filter them out; we should only emit a single notification per finished foreground process.) I was thinking about what'd be the best design for bash. You obviously know it way better, but here's my idea: - There could be a FGJOB_FINISHED_COMMAND (or something like that) similarly to PROMPT_COMMAND, which would be executed exactly once each time a foreground process completes. - There should be a reliable replacement for "history 1 | cut -c8-" to figure out the command line, for this I think the best could be a new argument to the "jobs" builtin (e.g. "-f" as foreground). Let us know if it makes sense. Thanks a lot, egmont On Mon, Mar 2, 2015 at 12:21 AM, Chet Ramey <chet.ra...@case.edu> wrote: > On 2/5/15 8:18 AM, Debarshi Ray wrote: > >> What do you think? Having bash offer a way to reliably find the last >> foreground command interactively run by the user would be helpful. >> > > I took a quick look at this, and I think what you want is $BASH_COMMAND > unless the last command is `fg'? If that's correct, we can think about > a way to make that information available. > > Chet > > -- > ``The lyf so short, the craft so long to lerne.'' - Chaucer > ``Ars longa, vita brevis'' - Hippocrates > Chet Ramey, ITS, CWRU c...@case.edu http://cnswww.cns.cwru.edu/~chet/