Beep in shell script not working?

Solution 1:

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

export BEEP=/usr/share/sounds/ubuntu/ringtones/Harmonics.ogg

Then have it available as a command

alias beep='paplay $BEEP'

Now just run beep whenever you need it. For example, to alert you when a command is finished:

find . | grep treasure ; beep

Solution 2:

First run sudo modprobe pcspkr and then beep 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 (using gksudo gedit perhaps) and comment out line that says blacklist pcspkr so it looks like this:

# ugly and loud noise, getting on everyone's nerves; this should be done by a
# nice pulseaudio bing (Ubuntu: #77010)
# blacklist pcspkr

Solution 3:

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:

sudo modprobe pcspkr

Automatically enable on boot:

sudo nano /etc/modprobe.d/blacklist.conf

(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,

xset b 100  # perhaps not needed, on my system it was 40 by default
pactl upload-sample /usr/share/sounds/ubuntu/stereo/bell.ogg bell.ogg

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:

sudo apt-get install beep

To automatically enable on boot, just add the above lines in your ~/.profile, or system-wide at /etc/profile


To test it:

printf '\a'

Beep!

beep

Buzz!

Solution 4:

To fix this problem persistently:

  • Run gconf-editor and if the desktop | gnome | peripherals | keyboard | bell_mode setting is present then change it from off to on
  • Run dconf-editor and if the org | gnome | settings-daemon | peripherals | keyboard | bell-mode setting is present then change it from off to on
  • Add pactl upload-sample /usr/share/sounds/gnome/default/alerts/glass.ogg bell.ogg to the file ~/.xprofile (you need gnome-control-center-data for glass.ogg)
  • Add [ "$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 the xset command in the terminal window in question.