10 Answers 10

up vote 90 down vote accepted

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

There are 3 ways of creating a sound from the beep manpage:

  1. 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.

  2. 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.

  3. 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.

  • 9
    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
  • 13
    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
  • 8
    @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
  • 3
    beep usually works out the box for debian but I needed to load the module for this to work on ubuntu – mchid Jun 28 '15 at 23:06
  • 4
    As mchid said, after adding the module: modprobe snd-pcsp, beep is now working! – lepe Sep 1 '15 at 1:19

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
  • 5
    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
  • 2
    This makes sense but it didn't work for me on Ubuntu 12.04. – isomorphismes Aug 24 '13 at 16:12
  • 2
    @isomorphismes: that's for consoles, not X11 terminals. For beeps under X, you may try this: askubuntu.com/a/587311/11015 – MestreLion Feb 19 '15 at 12:21
  • 1
    @isomorphismes: I'm also using Ubuntu 12.04, and for me sudo modprobe pcspkr was enough to enable beeps under the VT, either via printf "\a" or beep utility. – MestreLion Feb 20 '15 at 1:28
  • 3
    None of the methods work for me. Ubuntu 16.04.1 LTS – ar2015 Oct 26 '16 at 15:52

NOTE: This solution emits beeps from the speakers, not the motherboard.

ALSA comes with speaker-test, a command-line speaker test tone generator, which can be used to generate a beep:

$ speaker-test -t sine -f 1000 -l 1

See this arch linux forum thread.

However, the beep duration will be arbitrary, but can be controlled as follows:

$ ( speaker-test -t sine -f 1000 )& pid=$! ; sleep 0.1s ; kill -9 $pid

We can take it one step further and output a beep with this function:

_alarm() {
  ( \speaker-test --frequency $1 --test sine )&
  pid=$!
  \sleep 0.${2}s
  \kill -9 $pid
}

which is called with frequency and duration arguments:

$ _alarm 400 200

With this in mind, it is possible to create simple music with speaker-test. See this shell script.

  • This will make my loudspeakers beep, but not the PC speaker as the asker wants to! I need the latter to beep, because my cables went off and I must check if I soldered (+) and (-) the right way! – syntaxerror Mar 21 '15 at 18:42
  • 4
    But it will make my loudspeakers beep, which is what I want :D – Stefanos Kalantzis Oct 28 '15 at 14:08
  • 1
    How can you start such an function in Matlab? I am thinking about it here askubuntu.com/q/796639/25388 Your proposal is the only one which works in Macbooks with Linux. – Léo Léopold Hertz 준영 Jul 9 '16 at 11:00
  • Your function as one-liner (and Ctrl-C compatible): timeout ${2} speaker-test --frequency ${1} --test sine – user569825 Oct 23 '17 at 22:13
  • There's no real need to save pid, just speaker-test -t sine -f 1000 -l 1 & sleep .2 && kill -9 $! does the same thing. Anyway, @user569825's method is nicer. – erm3nda Jul 8 at 18:43
tput bel

because terminfo defines bel as

           Variable                       Cap-               TCap                  Description
            String                        name               Code

   bell                                   bel                bl                audible signal
                                                                               (bell) (P)

Some distros have command-line 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
  • 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
  • 2
    It would be more portable to use printf '\007' instead of echo. – Dennis Williamson Sep 14 '10 at 21:58

For using the soundcard if sox is installed and the PC speaker if not:

$ play -q -n synth 0.1 sin 880 || echo -e "\a"

sox is available for most distros.

  • 2
    +1 I did not know that play had this feature. I ended up using this in a then block within watch to notify me once a certain condition was met. Even lets me keep my headphones on. This was very useful to me. – andyortlieb May 12 '17 at 14:24

Try

echo -n Ctrl+V Ctrl+G

The downside is that this will work only 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.)

"Beep only works if your PC has a "speaker". Many modern laptops / small devices don't have one." -- tredegar .

Try playing a sound instead:

$ play xxxxx.wav

Worked for me, when all else failed.

References

On Linux, tools like beep can use an ioctl on the console device to emit a given sound. To be more specific, beep will use the KIOCSOUND ioctl, but there is also a KDMKTONE ioctl which can be used to generate sound.

As I understand it, the former starts a sound which lasts until it's explicitly cancelled, while the latter will create a beep of pre-determined duration. See the console_ioctl(4) man page for details.

So if you are unhappy with what beep does, you could write a few lines of code to access these ioctls directly. Assuming you have full access to /dev/console, which might well require root privileges.

In a terminal, press Ctrl+G and then Enter

  • 1
    In my terminal (gnome-terminal), it works without the Enter. Just as a test of PC speaker, this looks fine to me. It is utterly useless in scripts, though. – Palec Aug 27 '15 at 16:28
  • Surprising downvotes. This does work on some PC's, it might not work on all though. – Zzzach... Sep 7 '17 at 22:14
  • It doesn't work on rxvt, urxvt, xterm, lxterm or lxterminal, shouldn't my terminal at least flash if it has a visual bell? – Willem Nov 14 '17 at 13:44

protected by Community Aug 26 '15 at 20:40

Thank you for your interest in this question. Because it has attracted low-quality or spam answers that had to be removed, posting an answer now requires 10 reputation on this site (the association bonus does not count).

Would you like to answer one of these unanswered questions instead?

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