10

The default alias Alert is for the command

notify-send --urgency=low -i "$([ $? = 0 ] && echo terminal || echo error)" "$(history|tail -n1|sed -e '\''s/^\s*[0-9]\+\s*//;s/[;&|]\s*alert$//'\'')"

Executing alert gives an notification with text Alert and an terminal icon. Executing it with one param like alert !!!!! gives notification with text Alert !!!!! and !!!!!.

So, what's the difference between simple notify-send command and this complexed alias which uses notify-send, echo, history, tail and sed?

In which situations is this alias useful or was it just created for pun(Something like using sudo sudo sudo sudo sudo apt-get install

I'm using Ubuntu 12.10

3
  • The only thing I can think of is just to make creating a notification easier...maybe? Feb 20, 2014 at 13:14
  • @saiarcot895 try notify-send Linux Ubuntu it's much the same, the only difference is that alert shows a icon.But I don't understand why the command contains sed,tail and history Feb 20, 2014 at 13:21
  • For a detailed breakdown of how this works, see: saddlebackcss.github.io/tutorials/bash/2016/01/20/…
    – John K
    Jan 23, 2022 at 16:20

4 Answers 4

7

You could use the man pages to get details about what the commands combined here do. Here's a little about the purpose of those commands here:

"$([ $? = 0 ] && echo terminal || echo error)"

This would echo terminal or error as per to the execution status - successful or fail respectively of the last command; and the result is as the value to the -i switch of notify-send for displaying the icons.

history|tail -n1

..to get the last command executed.

and sed to parse the text to display it with notify-send message.


To understand these try the following:

true; echo "$([ $? = 0 ] && echo terminal || echo error)"

..this would echo terminal.

false; echo "$([ $? = 0 ] && echo terminal || echo error)"

..this would echo error.

notify-send -i terminal 'Please note the icon..' 'the default icon for the terminal has been used. You can specify any other icon of your choice'

And,

echo $?

..is very useful to know the exit value of the last command executed.

echo "$(echo "the output of this command enclosed within \$(...)") is being supplied here in the outer echo command where is used as an argument."

..nested echo as a simple demo for using $() in a command combo.

2
  • 1
    so this alias can be used to tell the user whether the command executed successfully or failed, but in an GUI way. Feb 20, 2014 at 13:42
  • yes it would notify about both the successful or unsuccessful executions graphically using the notify-send.. the exit status of last command is checked with the value of $?..I've included this in the edit..
    – rusty
    Feb 20, 2014 at 13:57
7

Let me try to explain what is happening here:

notify-send --urgency=low -i "$([ $? = 0 ] && echo terminal || echo error)" "$(history|tail -n1|sed -e '\''s/^\s*[0-9]\+\s*//;s/[;&|]\s*alert$//'\'')"
  • --urgency=low

    -u, --urgency=LEVEL Specifies the urgency level (low, normal, critical).
    
  • -i "$([ $? = 0 ] && echo terminal || echo error)"

    -i, --icon=ICON[,ICON...]
          Specifies an icon filename or stock icon to display.
    

    This part "$([ $? = 0 ] && echo terminal || echo error)". $? is the last error (or success) returned. So it returns text "terminal" if last command exit code was 0, without errors. Or returns "error" if exit code was not 0.

    And finally we get "terminal" or "error" icon.

  • $(history|tail -n1|sed -e '\''s/^\s*[0-9]\+\s*//;s/[;&|]\s*alert$//'\'')

    history|tail -n1 returns the last command from history.

    sed -e '\''s/^\s*[0-9]\+\s*//;s/[;&|]\s*alert$//'\'') this can be separated in 2 block of sed.

  • sed 's/^\s*[0-9]\+\s*//' remove all preceding spaces and tabs, all numerical after that, and also remove spaces and tabs at the end.

  • s/[;&|]\s*alert$// removes preceding symbols ; & |, any tabs and spaces and word "alert".

    It is just clean last executed command from symbols, and the word "alert" at the end.

    So if you use something like this:

    echo "Hello alert" | alert
    

    It will show alert with previous command.

4

I think the current answers explain how the inner workings of alert work (which is what I wanted to find out of curiosity and which got me here). But I think the original OP asks for what it is useful for which I'll try to explain as I understand from the commends above its declaration.

It is basically used to alert you when a command has finished when you can't sit watching the terminal the whole time waiting for it to finish. As per the commend example sleep 10; alert will show a notification of the command (sleep 10 in this case) with a terminal icon if it is successful (sleep 10 will take 10 seconds to complete).

From this you can see that it should be used as <command>; alert where you replace command with your command. I personally have to download a video everyday via wget because it fails periodically. So I just append the download command with alert to immediately notify me when it failed so that it can be continued again (my alert is modified to also beep to get my attention).

0

The reason sed, tail, and history are there is because it needs to get the message that you typed. (For some reason,) what you typed isn't directly available through any variable. Therefore, it has to use history to get the list of everything you've typed, tail -n1 to get the last thing you typed, and some regex to get rid of the "alert" command at the start.

Just to expand on this, there is a conditional command in there that tests to see if $? = 0 is true. From what I can tell, if it is true, then it is coming from the terminal, and should be regarded as a normal message. Otherwise, it should be considered an error, and an error icon will appear.

3
  • It doesn't get rid of alert, it will remain in the notification. Feb 20, 2014 at 13:35
  • 1
    If you answer with "for some reason" it is often not a very good answer ;)
    – Requist
    Feb 20, 2014 at 13:39
  • @Requist: I added "for some reason" for that specific statement because I was under the impression that $0 gives you the first argument for a command. It might be different in the case of an alias, though. Feb 20, 2014 at 14:11

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .