260

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

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

Stefan
  • 25,300
  • 1
    http://superuser.com/questions/47564/remotely-make-the-computer-beep-on-built-in-speaker – Ciro Santilli OurBigBook.com Sep 09 '15 at 20:47
  • http://superuser.com/questions/47564/remotely-make-the-computer-beep-on-built-in-speaker || http://askubuntu.com/questions/19906/beep-in-shell-script-not-working || http://stackoverflow.com/questions/10313939/how-to-emit-a-beep-on-my-computer-while-running-a-script-on-a-remote-machine – Ciro Santilli OurBigBook.com Sep 30 '15 at 14:51
  • 1
    I searched everywhere and tried everything I could to try and get this to work a few years ago, and finally gave up settling to play a sound file instead. But a bug must have been fixed. Because now when I open GNOME ALSA Mixer I see a new slider for 'Beep' with a muted checkbox. When enabled I now have a beep! Perhaps too much it beeps, lol, but my lovely Debian Buster finally beeps. It even beeps in TTY modes (via Ctl-Alt F1). Tested w/ something like echo -e "\a". – Elliptical view Nov 03 '20 at 04:41

12 Answers12

180

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.

user123456
  • 5,018
echox
  • 18,103
  • 17
    The homepage to the beep command is: http://johnath.com/beep/ On Ubuntu/Debian, you can install it with apt-get install beep. – Riccardo Murri Sep 13 '10 at 11:48
  • 19
    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
  • 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
  • 1
    I entered a string of mysql database imports on a single line in bash, and then found myself wishing there was some way to make linux notify me when they were all finished. Next time, I will have a way! Btw, I tried # beep on Centos 6, but it didn't work. # echo -ne '\007' did work though. (And so did # echo -e "\007") – Buttle Butkus Jul 08 '15 at 06:20
  • 5
    As mchid said, after adding the module: modprobe snd-pcsp, beep is now working! – lepe Sep 01 '15 at 01:19
  • 1
    What do you mean by open /dev/tty? I tried echo -ne "\007" > /dev/tty, and it produced no audible effect. – Jellicle Sep 08 '17 at 21:24
  • echo -ne "\007" works on a Ubuntu 18.04 box, but modprobe snd-pcs throws a a "FATAL" error – 42- Dec 10 '18 at 05:43
  • This answer assumes unrealistic environment... who has X server client today? and the 'beep' command is not built in bash (at least not in its Mac OS X version) – user176181 May 05 '20 at 06:31
  • Be warned that the Debian beep command only uses the internal speaker, so option 3 of this answer is ruled out. beep does not work on devices without a pc speaker. For example an Intel NUC does not have an internal speaker; you can emulate one by setting a BIOS option and plugging an external speaker in the front plug. – Francesco Potortì Jun 30 '20 at 11:09
127

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.

Ryne Everett
  • 2,383
93

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
slm
  • 369,824
Shawn Chin
  • 1,101
  • 6
    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
  • 2
    This makes sense but it didn't work for me on Ubuntu 12.04. – isomorphismes Aug 24 '13 at 16:12
  • 3
    @isomorphismes: that's for consoles, not X11 terminals. For beeps under X, you may try this: http://askubuntu.com/a/587311/11015 – MestreLion Feb 19 '15 at 12:21
  • @MestreLion Thank you. BTW, I had tried it in console (VT) as well. – isomorphismes Feb 19 '15 at 23:16
  • 2
    @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 01:28
  • This works fine on a Mac terminal. – aalaap Jun 10 '15 at 12:05
  • echo -e "\a" works on my remote terminal for CentOS 6. But I think I'll find it easier to remember echo -e "\007". -Bond, James Bond. – Buttle Butkus Jul 08 '15 at 06:22
  • 1
    The echo command is a shell built-in in many shells like dash, bash or zsh and will be used instead of the system provided /bin/echo program. Unforunately, some shells (especially POSIX shells like dash, which provides sh on some operating systems) do not offer the -e argument for echo. Thus, instead of recommending to run echo -e "\a" you should either recommend running /bin/echo -e "\a" (if the system has GNU echo installed) or printf "\a" or plainly echo "\a" which will also work in POSIX compliant shells because the shell interprets the backslash escape and not echo. – josch Feb 08 '16 at 14:16
  • 3
    None of the methods work for me. Ubuntu 16.04.1 LTS – ar2015 Oct 26 '16 at 15:52
  • @ar2015: Are you sure your PC has a speaker? This is not working for me either, but I never get beeps anyway. – einpoklum Nov 19 '16 at 19:44
  • @einpoklum, Yes, I do have a speaker both on my laptop and PC both under Ubuntu and none of the methods work on them. – ar2015 Nov 23 '16 at 04:09
  • This works if you have an X display as it opens up an xterm and plays the beep to it a number of times. Fiddle with the '3' value and the '0.25' to your hearts content: xterm -e sh -c 'for i in $(seq 3); do echo -e "\a"; sleep .25; done' – bgoodr Jun 15 '18 at 20:19
54
tput bel

because terminfo defines bel as

           Variable                       Cap-               TCap                  Description
            String                        name               Code

   bell                                   bel                bl                audible signal
                                                                               (bell) (P)
43

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.

slm
  • 369,824
Alexander
  • 9,850
  • 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
  • From a bash sh play will probably need sudo -u USER play... – intika Oct 02 '18 at 03:08
  • On my system, play uses pulseaudio, as a user. I guess it depends on system configuration. – Alexander Oct 02 '18 at 10:02
  • 2
    This is a great solution. Not only does it work without having to load any modules in any terminal but it's also very variable. You could have a script have different notifcation sounds very easily. Changing the 0.1 will change the length of the sound, lots of possibilities! (Make sure to run this script as the user under which pulseaudio is running, if you use pulseaudio. If you don't or can't, there are two workarounds. One is to run PA system wide (not recommended), one is to allow other users to access PA.) – confetti Dec 21 '19 at 13:34
  • 1
    if you get warning: play WARN alsa: can't encode 0-bit Unknown or not applicable then you should do export AUDIODRIVER=alsa to remove the warning. Cheers! – Yan King Yin Nov 21 '23 at 10:01
21

"Beep can only work if your PC has a traditional old style "speaker", and probably most if not all laptops and small devices don't have one.

However what they often have instead is a sound chip and one or more speaker(s) that can be used to make any sound you want.

So the outdated advise to install the beep command and/or the kernel module pcspkr will silently never work when you don't have the old style speaker hardware.

INSTEAD: Try playing a sound like this when you want a beep:

paplay /usr/share/sounds/sound-icons/capital

Note this uses the paplay (Pulse Audio Play) command which mixes better with other user level (user app) sounds on your system, and not the older aplay (ALSA Play) command which generally can only play one sound at the same time. But note however, that PulseAudio calls ALSA to actually play the sound.

My previous suggestion to use play might still work, but running SoX of which play is from, is overkill.


Works for me when all else failed. Thanks to: tredegar & hk_centos and others.

Elliptical view
  • 3,921
  • 4
  • 27
  • 46
  • 2
    Nice! The command is available on a fresh (rather) installation of Linux Mint 20. – Krzysztof Tomaszewski Sep 07 '21 at 21:56
  • Now that PipeWire has replaced PulseAudio, should this answer be updated? Or, should we just go back to using play from sox since it works the same way on all Unix systems? – hackerb9 Oct 18 '23 at 05:12
14

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
slm
  • 369,824
6

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.

slm
  • 369,824
MvG
  • 4,411
2

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

codehead
  • 4,960
2

The only solution that worked for me on mint (thanks to @alexander above)

alias beep='play -q -n synth 0.1 sin 880 >& /dev/null'
zzapper
  • 1,140
1

In a terminal, press Ctrl+G and then Enter

Michael Mrozek
  • 93,103
  • 40
  • 240
  • 233
-1

KDE Plasma (5.18) Konsole app

Settings (menu) > Configure Notifications > Bell in Focused Session

Check "play a sound" and select an associated audio file

echo -e "\a"

etc should then work.

Bob
  • 183