Use of default alias "alert"

Solution 1:

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.

Solution 2:

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.

Solution 3:

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).