
Learn through the super-clean Baeldung Pro experience:
>> Membership and Baeldung Pro.
No ads, dark-mode and 6 months free of IntelliJ Idea Ultimate to start with.
Last updated: March 18, 2024
There are times where we might want to make our PC speaker beep, such as to test if our speaker is working or tell us when a command or script is finished. We might even just want to play around with the speaker to create a song or melody.
In this tutorial, we’ll explore several Linux commands to make a beep sound. All commands are usually already installed on common Linux distributions and are available on most Linux core repositories.
Three common Linux commands that we can use to make a beep sound are echo, printf, and tput.
Let’s send a beep sound to a speaker twice using echo:
$ echo -e '\a'
$ echo -e '\007'
Parameters that we passed to the command:
The command didn’t print anything on the standard output but sent a beep sound to the speaker.
printf is similar to echo, it’s mostly used to format and display text to the standard output. Let’s try it as an alternative to echo:
$ printf '\a'
$ printf '\007'
Our speaker should have beeped with each command.
The tput command writes string parameter to the standard output:
$ tput bel
tput queried terminfo database for the ‘bel‘ string, which returned ‘audible signal’, then sent it to the speaker to make a beep sound.
paplay is a tool for playing back audio files:
$ paplay /usr/share/sounds/freedesktop/stereo/bell.oga
$ paplay /usr/share/sounds/gnome/default/alerts/drip.ogg
$ paplay /usr/share/sounds/mate/default/alerts/drip.ogg
The audio files that we played, drip.ogg or bell.oga, sound like a beep sound. However, there are many audio files usually found in the /usr/share/sounds directory that we can experiment with.
speaker-test generates a tone that we can use to test a computer speaker:
$ speaker-test -t sine -f 1000 -l 1 & sleep .2 && kill -9 $!
The command above will make a tone with a frequency of 1000Hz for 0.2 seconds. speaker-test runs for a minimum of 4 seconds every time we generate a tone, hence the reason why we added the sleep and kill commands.
We can also run a 6-channel speaker test in WAV mode:
$ speaker-test -c 6 -t wav -l1
The command above plays the WAV files for each audio channel.
beep is a small program to send a beep sound to the PC speaker. It has many options that we can use to create melodies, specifically the ones to set the tone frequency, duration, repetition, and delay in between tones.
beep is available on many Linux core repositories. For example, to install it on Debian and its derivatives:
$ apt install beep
On Fedora:
$ dnf -y install beep
On Arch:
$ pacman -Sy
$ pacman -S beep
Let’s send a tone to a speaker:
$ beep -l300 -f750
The parameters:
The beep command may not work on recent machines, but there are a few things we can try to overcome that.
Firstly, we need to enable the kernel module for the built-in speaker:
$ sudo modprobe pcspkr
Secondly, on Debian and its derivatives, there’s a blacklist file (/etc/modprobe.d/blacklist) to disable the PC speaker module. In case the file exists, we need to remove or comment ‘blacklist pcspkr‘ line in that file.
If, at this point, the beep command still doesn’t beep, we might need to use another command.
The play command is available in the sox package. SoX, or Sound eXchange, provides a ton of functionalities for audio manipulation, such as:
For this, we’ll only explore how to make a beep sound and manipulate the frequency and duration of a tone.
SoX is available on many Linux core repositories. For example, on Debian and its derivatives:
$ apt install sox
On Fedora:
$ dnf -y install sox
On Arch:
$ pacman -Sy
$ pacman -S sox
Let’s send a beep sound to the speaker:
$ play -n synth 0.1 sine 880 vol 0.5
SoX, as its manual page says, is the Swiss Army knife of audio manipulation. We can also use it to generate a guitar chord:
$ play -n synth pl G2 pl B2 pl D3 pl G3 pl D4 pl G4 \
delay 0 .05 .1 .15 .2 .25 remix - fade 0 4 .1 norm -1
Now that we know how to send a beep tone to a speaker using several commands, let’s explore it a bit further. We’re going to make a beep sound when a command or script is finished, make a remote machine beep, and make melodies.
Let’s use the echo command to make a beep sound when a command is finished:
$ ls -l
total 0
$ wget https://www.tweetails.com/result.php?taskid={3900000..3900002} && echo -e '\a'
$ ls -l
total 28
-rw-r--r-- 1 baeldung baeldung 1345 Oct 4 18:37 'result.php?taskid=3900000'
-rw-r--r-- 1 baeldung baeldung 9441 Oct 4 18:37 'result.php?taskid=3900001'
-rw-r--r-- 1 baeldung baeldung 9469 Oct 4 18:37 'result.php?taskid=3900002'
The wget command above downloaded three pages from the server and stored them in files. Afterward, the echo command made a beep sound.
If we need to make a remote computer beep because we couldn’t find a specific box among many others and have forgotten which IP is which box:
$ ssh user@remote-machine
$ sudo sh -c "echo -e '\a' > /dev/console"
We need to run the command as root as we’re doing the redirecton to /dev/console:
$ ls -l /dev/console
crw------- 1 root root 5, 1 Sep 26 09:42 /dev/console
We might also need to enable the kernel module for the built-in speaker and remove it from the blacklist file (/etc/:
$ sudo modprobe pcspkr
Since we can configure the frequency and duration of a tone using beep or play command, let’s make melodies.
For example, we can create Imperial March melody:
$ beep -l 350 -f 392 -D 100 \
-n -l 350 -f 392 -D 100 -n -l 350 -f 392 -D 100 \
-n -l 250 -f 311.1 -D 100 -n -l 25 -f 466.2 -D 100 \
-n -l 350 -f 392 -D 100 -n -l 250 -f 311.1 -D 100 \
-n -l 25 -f 466.2 -D 100 -n -l 700 -f 392 -D 100 \
-n -l 350 -f 587.32 -D 100 -n -l 350 -f 587.32 -D 100 \
...
Similarly, a Super Mario melody using the same beep command:
$ beep -f330 -l137 \
-n -f330 -l275 -n -f330 -l137 -d137 \
-n -f262 -l137 -n -f330 -l275 \
-n -f392 -l550 -d550 -n -f262 -l412 \
-n -f196 -l137 -d275 -n -f164 -l137 -d137 \
-n -f220 -l275 -n -f247 -l137 -d137 \
...
Besides generating a tone, the play command can generate a guitar sound. Let’s use it to create Pachelbel’s Canon melody:
$ play -n synth \
pl F4 pl A4 pl C5 pl F5 pl C4 pl G4 pl C5 pl E5 pl D4 pl F4 \
pl A4 pl D5 pl A3 pl F4 pl A4 pl C5 pl F5 pl C4 pl G4 pl C5 \
pl E5 pl D4 pl F4 pl A4 pl D5 pl A3 pl E4 pl A4 pl C5 pl Bb3 \
pl D4 pl F4 pl Bb4 pl F3 pl C4 pl F4 pl A4 pl Bb3 pl D4 pl F4 \
pl Bb4 pl C4 pl E4 pl G4 pl C5 pl F4 pl A4 pl C5 pl F5 pl C4 \
pl G4 pl C5 pl E5 pl D4 pl F4 pl A4 pl D5 pl A3 pl E4 pl A4 \
pl C5 pl Bb3 pl D4 pl F4 pl Bb4 pl F3 pl C4 pl F4 pl A4 pl Bb3 \
pl D4 pl F4 pl Bb4 pl C4 pl E4 pl G4 pl C5 \
pl A4 pl G4 pl F4 pl E4 pl D4 pl C4 pl D4 pl E4 \
delay \
0.175 +0.175 +0.175 +0.175 +0.175 +0.175 +0.175 +0.175 +0.175 +0.175 \
+0.175 +0.175 +0.175 +0.175 +0.175 +0.175 +0.175 +0.175 +0.175 +0.175 \
+0.175 +0.175 +0.175 +0.175 +0.175 +0.175 +0.175 +0.175 +0.175 +0.175 \
+0.175 +0.175 +0.175 +0.175 +0.175 +0.175 +0.175 +0.175 +0.175 +0.175 \
+0.175 +0.175 +0.175 +0.175 +0.175 +0.175 +0.175 +0.175 +0.175 +0.175 \
+0.175 +0.175 +0.175 +0.175 +0.175 +0.175 +0.175 +0.175 +0.175 +0.175 \
+0.175 +0.175 +0.175 +0.175 +0.175 +0.175 +0.175 +0.175 +0.175 +0.175 \
+0.175 +0.175 +0.175 +0.175 +0.175 +0.175 +0.175 \
+0.700 +0.700 +0.700 +0.700 +0.700 +0.700 +0.700 +0.700 \
\
remix - fade 0 20 .1 norm -1
Furthermore, we can save the melody above into a file by using sox command:
$ sox -n pachelbelcanon.wav synth \
pl F4 pl A4 pl C5 pl F5 pl C4 pl G4 pl C5 pl E5 pl D4 pl F4 \
pl A4 pl D5 pl A3 pl F4 pl A4 pl C5 pl F5 pl C4 pl G4 pl C5 \
...
remix - fade 0 20 .1 norm -1
Then we can play it with our favorite music player, or for example, with paplay:
$ paplay pachelbelcanon.wav
The longer versions of the melodies we played above are available in their original repositories.
Linux has many commands or tools that we can use to make a noise with our speaker, e.g., from just a simple beep sound; to chime and guitar sounds.
In this article, we learned about several Linux commands to make a beep sound.
Some commands provide functionality to change the frequency and duration of the tone that it generates, which enables us to utilize them for some real-life use cases, such as to beep at the end of a command, make a remote machine beep, or make melodies.