In a bash script/command how can I make a PC beep noise, or play a sound file?

ShellAudio

Shell Problem Overview


I have some long running scripts with breaks requiring input/interaction to continue but when I switch to another window I'd like to be notified (by sound) that a task is complete and now awaiting input.

I would prefer to be able to play an audio clip (*.mp3, *.ogg, etc.) but wouldn't care if the only solution is to make the PC Speaker beep noise.

Any ideas? I'm open to any CLI utilities I can install that play sounds that in turn I can execute when needed.

FYI: My System is running WinXP Pro.

UPDATE: Doh! My Windows > Control Panel > Sounds > Default Beep: was set to (none). Grrr...

Problem solved.

Shell Solutions


Solution 1 - Shell

This will make a beep from within bash

echo -en "\007"

Solution 2 - Shell

Try this:

echo ^G

(^G is obtained by ctrl+G).

Note: you can't copy and paste this code in a batch file, it won't work. To obtain a ^G character in a file, type in a cmd window:

echo ^G > beep.txt

(again, ^G is obtained by ctrl+G).

Then you'll have a file named beep.txt, open it with notepad, there will be a square character. This is our ^G once it is saved in a file.

You can then copy and paste it in a batch file to make a sound (don't forget to put "echo" in front of it).

Solution 3 - Shell

spd-say

sleep 2; spd-say 'get back to work'

Infinite loop with -w if you need extra motivation:

sleep 2; while true; do spd-say -w 'get back to work'; done

or if you prefer the carrot:

sleep 2; while true; do spd-say -t female1 -w "I'm done, come back to me, darling"; done

Pre-installed on Ubuntu 14.04 via the package speech-dispatcher: http://releases.ubuntu.com/trusty/ubuntu-14.04.4-desktop-amd64.manifest for blind people I suppose?

See also: https://askubuntu.com/questions/277215/how-to-make-a-sound-once-a-process-is-complete

Also add a popup

This combo is a life saver, b stands for beep:

b() ( spd-say 'done'; zenity --info --text "$(date);$(pwd)" & )

and then:

super-slow-command;b

If I'm somewhere in the room, I'll hear it and know that the long job is done.

Otherwise, I'll see the popup when I get back to my computer.

Related: https://stackoverflow.com/questions/7035/how-to-show-a-gui-message-box-from-a-bash-script-in-linux

Listen to your cooler

I'm joking of course, but for compilation I noticed that I use often use this queue subconsciously. When the cooler stops humming for a while, it means that the compilation is over!

Solution 4 - Shell

By setting this variable as follows

PROMPT_COMMAND="echo -en '\a'"

then bash will beep every time it shows the prompt. When you do not need it anymore,

unset PROMPT_COMMAND

Solution 5 - Shell

I know your question was for Window but just putting this here for any Mac OSX users who come across this article. OSX 10+ comes with the say command:

say "I'm done"

For example:

sleep 5 && say "I'm done waiting 5 seconds"

Solution 6 - Shell

>copy con beep.bat [Enter] > >@echo off [Enter] > >echo [Ctrl+G] [Enter] > >[Ctrl+Z] [Enter] > >beep.bat [Enter]

Solution 7 - Shell

To play the system sound from Windows command line you can run:

rundll32 user32.dll,MessageBeep

It should work on all version of Windows.

Solution 8 - Shell

Simple answer without ^G

echo -en "\007"

Solution 9 - Shell

In my bash profile I've added a BEEP to the script using @GregReynolds solution above then added this to PS1:

GREEN="\[\033[0;32m\]"
BEEP=$(echo -en "\007")
export PS1="$GREEN : ${BEEP}"

source ~/.bash_profile - you should hear the beep after the command prompt returns

I have git-autocomplete on usually so I've provided a much simplified version above

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionscunliffeView Question on Stackoverflow
Solution 1 - ShellGreg ReynoldsView Answer on Stackoverflow
Solution 2 - ShellFWHView Answer on Stackoverflow
Solution 3 - ShellCiro Santilli Путлер Капут 六四事View Answer on Stackoverflow
Solution 4 - Shellam70View Answer on Stackoverflow
Solution 5 - ShellJay El-KaakeView Answer on Stackoverflow
Solution 6 - ShellnaveenView Answer on Stackoverflow
Solution 7 - Shellviktor-zinView Answer on Stackoverflow
Solution 8 - ShellGlennGView Answer on Stackoverflow
Solution 9 - ShellRob EvansView Answer on Stackoverflow