6

Is it possible to automatically add some characters at the end of each command typed in the terminal.

For example, if I write

sudo apt-get update

the shell will execute

sudo apt-get update && notify-send Done

without me writing the && notify-send Done part.

Thanks in advance!

  • 1
    Not what you want but while waiting for a better solution you could use aliases. E.g. add the line alias ok='notify-send Done' to your ~.bashrc and then write your command like this: sudo apt-get update && ok - That also saves some typing and you can control whether you need a notification or not. – Byte Commander Jul 9 '15 at 17:38
  • @ByteCommander Well yeah, I'll do that until I find better solution. Thanks! – ioanD Jul 9 '15 at 17:39
  • 1
    or just && alert – Sylvain Pineau Jul 9 '15 at 17:40
  • @SylvainPineau I like to be able to customise the output, so alert doesn't work for me – ioanD Jul 9 '15 at 17:41
  • It's vague to me..is the part after && (including) always going to change? Can it be empty? What about the first command ? Do you just want to type the first part or a shortcut of second part? Will the first command be changed? – heemayl Jul 9 '15 at 18:07
6

If you're using bash as your default shell, you can set PROMPT_COMMAND.

From the bash man page:

PROMPT_COMMAND
If set, the value is executed as a command prior to issuing each primary prompt.

So just paste the following line in your .bashrc to get a notification for each command:

PROMPT_COMMAND="notify-send Done"

So each time your bash prompt is called (normal behavior when a command is finished, with success or not), you'll get a notification.

Note: you'll be also notified if you press just Enter as the prompt will be displayed again.

  • 1
    For the after case, trap "notify-send Done" DEBUG might be more appropriate. – muru Jul 9 '15 at 18:05
  • @muru thanks for the suggestion but tested with sleep 3, the notification does not wait 3s. – Sylvain Pineau Jul 9 '15 at 18:24
0

You can bind some characters to to an enter key:

$ bind '"\C-M":" && echo test\n"'

This has some drawbacks, for example if you just press Enter:

-bash: syntax error near unexpected token `&&'

You can block this effect by terminating your lines with # character - it will turn appended text into a harmless comment.

Still I'd say that this sounds like a really bad idea.

  • I wanted to play around with this, but so far I haven't found the syntax to unset it after I'm done. I was thinking there must be some placeholder like : which would get rid of the syntax error. Still, it begs the question as to why anyone would want to do this for every command. Something more focused/selective would probably be a lot more useful. – Joe Jul 16 '15 at 0:37
  • @Joe, use bind '"\C-M":"\n"' # to unset this. The trailing hash in the command makes the appended text a comment thus disabling its effect, just as I wrote in the answer. – Nykakin Jul 16 '15 at 7:16

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.