On , I learnt ...

About Ubuntu’s alert Bash alias

Ubuntu’s default ~/.bashrc includes an alert alias for triggering a desktop notification when a long-running processes has completed:

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

(Command wrapped for clarity).

Example usage:

$ ./run_test_suite.sh ; alert

It uses the exit code of the previous process $? to determine the message and icon displayed. As you can see, it uses notify-send to create the alert — here’s a more detailed explanation of how it works.

You can create a macOS equivalent with the Homebrew-installed terminal-notifier:

alias alert='terminal-notifier
    -title "Process finished"
    -message "$([ $? = 0 ] && echo Success || echo Error)"'

(Command wrapped for clarity again).

This is a basic version: you can make use of terminal-notifier’s options to add sounds, icons and even activate iTerm2 again with -activate com.googlecode.iterm2.

The appearance of the terminal-notifier notifications can be configured in System Preferences > Notifications:

terminal-notifier preferences