Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

Using bash, how can I make the pc speaker beep?

Something like echo 'beepsound' > /dev/pcspkr would be nice.

share|improve this question

7 Answers 7

up vote 27 down vote accepted

I usually use the little utility beep installed on many systems. This command will try different aproaches to create a system sound.

3 ways of creating a sound from the beep manpage:

The traditional method of producing a beep in a shell script is to write an ASCII BEL (\007) character to standard output, by means of a shell command such as ‘echo -ne '\007'’. This only works if the calling shell's standard output is currently directed to a terminal device of some sort; if not, the beep will produce no sound and might even cause unwanted corruption in whatever file the output is directed to.

There are other ways to cause a beeping noise. A slightly more reliable method is to open /dev/tty and send your BEL character there. This is robust against I/O redirection, but still fails in the case where the shell script wishing to generate a beep does not have a controlling terminal, for example because it is run from an X window manager.

A third approach is to connect to your X display and send it a bell command. This does not depend on a Unix terminal device, but does (of course) require an X display.

Beep will simply try these 3 methods.

share|improve this answer
4  
The homepage to the beep command is: johnath.com/beep On Ubuntu/Debian, you can install it with apt-get install beep. –  Riccardo Murri Sep 13 '10 at 11:48
3  
On linux, there is a fourth badass method for beeping: unload the pcspkr module, load the snd-pcsp, and you now have a alsa soundcard that uses old-school tricks to actually feed digital sound to your internal speaker. It gives crappy results with piezoelectric internal speakers, but on classical internal speakers, the quality is quite good for what it is. This way, you can get much more creative for your beep sounds ;) –  BatchyX Jan 19 '13 at 15:18
    
@BatchyX In Ubuntu 12.04 I apt-cache searched for pcspkr and snd-pcsp with no results. apt-cache search pc speaker gives some results but nothing that looks relevant. –  isomorphismes Aug 24 '13 at 16:13
1  
@isomorphismes: pcspkr and snd-pcsp are modules, not packages. use modprobe/modprobe -r/lsmod to manipulate them. For your information, they are in the linux-image-something package, which is hopefully already installed (or else you would have no linux kernel). –  BatchyX Aug 24 '13 at 16:17
    
Oh awesome info, thank you @BatchyX. –  isomorphismes Aug 26 '13 at 2:47

Simply echoing \a or \07 works for me.

echo -e "\a"

This will probably require the pcspkr kernel module to be loaded. I've only tested this on RHEL, so YMMV.

Update

As Warren pointed out in the comments, this may not work when logged in remotely via SSH. A quick workaround would be to redirect the output to any of the TTY devices (ideally one that is unused). E.g.:

echo -en "\a" > /dev/tty5

Happy beeping!

share|improve this answer
1  
If you're using an X terminal or ssh'd into the machine, this may just cause the terminal to flash, since many xterm/vt100 type programs are configured to do that for BEL characters. –  Warren Young Sep 13 '10 at 10:52
    
@warren. Good point, will update answer. –  Shawn Chin Sep 13 '10 at 13:20
    
This makes sense but it didn't work for me on Ubuntu 12.04. –  isomorphismes Aug 24 '13 at 16:12

Some distros have cmdline utilities to achieve this. Maybe you could tell us what distro you are on.
Or search (e.g. emerge -s beep in gentoo).

Going beyond "available" utils, you could also make a perl script that emits the beep, all you need to do is include:

<SomeCodeBefore>
print "\007";
<SomeCodeAfter>  

If you do end up getting 'beep', try out the following:

#! /bin/sh 

beep -f 500 -l 700 
beep -f 480 -l 400 
beep -f 470 -l 250 
beep -f 530 -l 300 -D 100 
beep -f 500 -l 300 -D 100 
beep -f 500 -l 300 
beep -f 400 -l 600 
beep -f 300 -l 500 
beep -f 350 -l 700 
beep -f 250  -l 600  
share|improve this answer
    
I like my questions to remain distro-agnostic, would echo -e "\007" work in bash instead of print "\007 in perl? –  Stefan Sep 13 '10 at 10:35
1  
@Stefan - Beep is pretty much 'universal'. You can, however, remain distro agnostic by using 'echo -e "\a"' or 'echo -e "\07"'. Cheers! –  wormintrude Sep 13 '10 at 19:13
    
It would be more portable to use printf '\007' instead of echo. –  Dennis Williamson Sep 14 '10 at 21:58

Try echo -n Ctrl+V Ctrl+G The downside is that this will only work when the output device is a terminal, so it may not work inside a cron job, for instance (But if you are root you might be able redirect to /dev/console for immediate beeping).

share|improve this answer

Try the following code in the command line:

echo -e "\a"
share|improve this answer
1  
Welcome to U&L. Please have a look at the answers already there before posting your own. You always can edit or comment on answers that are similar to yours (once you have enough reputation). But this doesn't differ from the answer ShawnChin posted over 3 years ago. Except giving fewer details, that is. –  Adaephon Jun 16 at 11:36
tput bel

because terminfo defines bel as

           Variable                       Cap-               TCap                  Description
            String                        name               Code

   bell                                   bel                bl                audible signal
                                                                               (bell) (P)
share|improve this answer

In a terminal, press Ctrl+G and then Enter

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.