1

I was scrolling through my bashrc (bored) and I got interested in the alert command that is concatenated in there by default. It's practical and everything, but it requires you to type ; alert at the end of every comand you want to be notified about.

Is there a smart way of turning this alias into something like a universal command option?

Examples:

sudo apt update -a 
cp <someBigFile> <someOtherPlace> -a

etc. (end of thinking capacity :)

I know that I can't necessarily use "-a", as it might be an existing option for some commmands, it's just an example I put up there to clarify.

1

I can think of two ways how to achieve it. But TBH I'm using the same approach as You described. command; alert.

1. create function which calls Your command You want to be notified after it finishes.

function alert(){
        $@
        echo -en "\a"
}

So running alert sudo apt-update will ring a bell after it finishes.

2. function to set/unset alerting and call it with PROMPT_COMMAND

function alert(){
        case $1 in
                "")
                        [[ -n $alert ]] && echo -en "\a";;
                set)
                        export alert=true;;
                unset)
                        unset alert;;
        esac
}

# add function alert to your PROMPT_COMMAND
PROMPT_COMMAND='alert'

then You can enable alerts:

alert set
command1
command2
alert unset

2b. both combined

function alert(){
        case $1 in
                "")
                        [[ -n $alert ]] && echo -en "\a";;
                set)
                        export alert=true;;
                unset)
                        unset alert;;
                *)
                        $@
                        echo -en "\a";;
        esac
}

# add function alert to your PROMPT_COMMAND
PROMPT_COMMAND='alert'

both ways described above: alert sudo apt update or

alert set
command1
command2
aler unset
  • Nice! I like both ideas :D Those are getting added to my bashrc. Thx for the help. If I understand correctly you can make a sort of concatenation of both solutions right? – Ciarán J. Hagen Feb 25 at 12:03
  • what do you mean with "make a sort of concatenation"? – Jakub Jindra Feb 25 at 12:14
  • 1
    I could add version one as an else clause to option two... So basically put "set" or "unset" or "<other command>" as cases. Or am I completely off here and missing omething? – Ciarán J. Hagen Feb 25 at 12:19
  • yes, sure add another case matching everything else. – Jakub Jindra Feb 25 at 12:25

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.