Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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

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

share|improve this question
up vote 46 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
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
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
@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
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

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.


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
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
@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
@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

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

share|improve this answer
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
But it will make my loudspeakers beep, which is what I want :D – Stefanos Kalantzis Oct 28 '15 at 14:08
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. – Masi Jul 9 at 11:00

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:

print "\007";

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
@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
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


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

share|improve this answer

Try the following code in the command line:

echo -e "\a"
share|improve this answer
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 '14 at 11:36

In a terminal, press Ctrl+G and then Enter

share|improve this answer
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

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.