I'd like to use a beep sound in a shell script. Unfortunately none of the methods I found via Google work for me.
I tried
echo -e '\a'
echo -ne '\007'
and the command beep
after I installed it via apt.
What could be the reason?
I'd like to use a beep sound in a shell script. Unfortunately none of the methods I found via Google work for me.
I tried
echo -e '\a'
echo -ne '\007'
and the command beep
after I installed it via apt.
What could be the reason?
Not being a fan of the pcspkr beep, I use a beep from one of the system sounds with the installed pulseaudio server's paplay command.
First find a sound you like (you can browse /usr/share/sounds for some available ones for example) and create a reference to it
Then have it available as a command
Now just run
beep
whenever you need it. For example, to alert you when a command is finished:First run
sudo modprobe pcspkr
and thenbeep
should work.The reason this doesn't is because by default Ubuntu no longer loads the hardware driver that produce beeps.
If this works for you then to enable the loading of pcspkr permanently edit the
/etc/modprobe.d/blacklist.conf
file (usinggksudo gedit
perhaps) and comment out line that saysblacklist pcspkr
so it looks like this:Since this is a very high rated question on google, I'll add the steps I did to re-enable beep in both console and X11:
For the Linux Console (CTRL+ALT+F1...F6):
Why it does not work by default
As already answered, the
pcspkr
kernel driver for the PC Speaker is blacklisted in Ubuntu.Temporarily enable until reboot:
Automatically enable on boot:
(delete or comment
pcspkr
line by prepending it with#
)For X11 terminals (such as the default
gnome-terminal
)Why it does not work by default
Under X, like when using Unity, KDE, Gnome Shell, the beep events are captured by PulseAudio thanks to
module-x11-bell
, which is loaded by default at/usr/bin/start-pulseaudio-x11
. And the sound sample PulseAudio plays on beep,bell.ogg
, is blank by default. Additionally, the bell volume may be muted.To temporarily enable for current session,
There are other suitable samples you can try at
/usr/share/sounds
, for example check the ones at/usr/share/sounds/gnome/default/alerts/
Note that the
beep
program is not really necessary. But if installed, it uses the PC Speaker. It was the only way I could find to enable the buzzer under X:To automatically enable on boot, just add the above lines in your
~/.profile
, or system-wide at/etc/profile
To test it:
Beep!
Buzz!
I've encountered this problem before. From what I remember, the problem is that the terminal bell tries to ring an internal computer speaker (as in an old-school desktop) but laptops and some newer computers are missing such a thing.
The only solution I found at the time was to
and
e.g.
To fix this problem persistently:
gconf-editor
and if thedesktop | gnome | peripherals | keyboard | bell_mode
setting is present then change it fromoff
toon
dconf-editor
and if theorg | gnome | settings-daemon | peripherals | keyboard | bell-mode
setting is present then change it fromoff
toon
pactl upload-sample /usr/share/sounds/gnome/default/alerts/glass.ogg bell.ogg
to the file~/.xprofile
(you needgnome-control-center-data
forglass.ogg
)[ "$DISPLAY" ] && xset b 100
to the file~/.bashrc
The simplest way to activate this solution is to reboot.
Further, to implement this solution immediately for a terminal window that is already open, run the
pactl
command and run thexset
command in the terminal window in question."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 modulepcspkr
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:
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 olderaplay
(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.
I finally found a solution, which doesn't require
alsamixer
to have a PC Beep option. I think I remember all my changes:uncomment the following in
/etc/pulse/default.pa
:per this bug, run
pactl upload-sample /usr/share/sounds/ubuntu/stereo/bell.ogg bell.ogg
As far as I can tell, this is a bug: System beep broken in Karmic despite heroic efforts to fix it.
If you have actual speakers connected to the computer and you're not getting a beep it's likely because you are using compiz. Compiz is relying on pulseaudio catching the beeps and playing them while metacity bypasses the usual setup and uses libcanberra to play a beep sound. If it works with metacity and not compiz that is your problem, otherwise the answer htorque gave is corrent.
An alternative approach - set your xterm / console to "Visual Bell" so that when it would beep, the window simply inverts its colours for a short time.
I have a bash function called beep to get my attention once a command is finished.
And it is used this way