Ubuntu – Automatically end each command in terminal with a different message or sound

bashcommand linesound

I installed SVOX pico2wave and created a script.
Now when I enter:

speech "Hello world"

The computer text-to-voice engine says out loud "Hello world". Furthermore, I use in .bashrc the variable PROMPT_COMMAND defined as:

PROMPT_COMMAND="speech 'Command executed.'"

So the nice text-to-voice engine announces me loud that the command is executed, before returning the prompt in the terminal.

However, this gets boring after a while and I thought it would be really cool to end each command in terminal with the computer speaking a different line of text (think of the autopilot of Starship Enterprise).

So I created in .bashrc an array variable, with different messages:

array[0]="Shields at 90%"
array[1]="Engaging proton overdrive"
array[2]="Autopilot disengaged"
array[3]="Targetting solution available"
array[4]="Alert. Incoming missile."
array[5]="Deploying countermeasures."
array[6]="Firing torpedoes."
array[7]="Engaging auto-cannon."
array[8]="Severe damage on deck 17. Sealing off."
array[9]="Deploying repair droids to deck 17."

What I would like, is that each time I run a command in terminal, the variable PROMPT_COMMAND to be updated and read a different random line in the array.

I presume I need a loop, but I didn't know how to make it.

I would be most grateful for any help. Thank you.

Best Answer

Create a script and save it somewhere which contains your lines and the logic to choose a random line from your array and calling speech command on that line:

array[0]="Shields at 90%"
array[1]="Engaging proton overdrive"
array[2]="Autopilot disengaged"
array[3]="Targetting solution available"
array[4]="Alert. Incoming missile."
array[5]="Deploying countermeasures."
array[6]="Firing torpedoes."
array[7]="Engaging auto-cannon."
array[8]="Severe damage on deck 17. Sealing off."
array[9]="Deploying repair droids to deck 17."

line=${array[$RANDOM % ${#array[@]}]}
speech "$line"

Then in your .bashrc or .profile set PROMPT_COMMAND:

PROMPT_COMMAND="bash $HOME/PATH/TO/myscript.sh"

Just replace the speech with echo to get a messge instead of the voice.