How do I make my pc speaker beep

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

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

Asked By: Stefan

||

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 "07";
<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
Answered By: wormintrude

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

Answered By: codehead

Simply echoing a or 7 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
Answered By: Shawn Chin

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 (07) character to standard output, by means of a shell command such as

    echo -ne '07'
    

    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.

Answered By: echox

In a terminal, press Ctrl+G and then Enter

Answered By: Hitesh Chechani
tput bel

because terminfo defines bel as

           Variable                       Cap-               TCap                  Description
            String                        name               Code

   bell                                   bel                bl                audible signal
                                                                               (bell) (P)
Answered By: Winston Smith

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.

Answered By: Ryne Everett

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.

Answered By: MvG

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.

Answered By: Alexander

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.

Answered By: Elliptical view

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'
Answered By: zzapper

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.

Answered By: Bob
Categories: Answers Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.