1. Overview

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.

2. Using Linux Standard Outputs

Three common Linux commands that we can use to make a beep sound are echo, printf, and tput.

2.1. echo

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:

  • e: enable interpretation of backslash escapes
  • ‘\a’: alert or BEL character
  • ‘\007’: BEL character in hex format

The command didn’t print anything on the standard output but sent a beep sound to the speaker.

2.2. printf

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.

2.3. tput

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.

3. Using paplay Command

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.

4. Using speaker-test Command

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.

5. Using beep Command

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.

5.1. Installing beep

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

5.2. Beep at a Certain Frequency and Duration

Let’s send a tone to a speaker:

$ beep -l300 -f750

The parameters:

  • l: tone duration in milliseconds
  • f: frequency of the tone in Hz, where 0 < f < 20,000

5.3. beep Is Not Beeping

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.

6. Using play Command

The play command is available in the sox package. SoX, or Sound eXchange, provides a ton of functionalities for audio manipulation, such as:

  • play an audio song
  • play an audio song backwards
  • record sound
  • convert audio file format
  • combine multiple audio files into a single file
  • trim audio file
  • and many more

For this, we’ll only explore how to make a beep sound and manipulate the frequency and duration of a tone.

6.1. Installing the sox Package

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

6.2. Beep at a Certain Frequency and Duration

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

7. Implementation

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.

7.1. Beep When a Command Is Finished

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.

7.2. Make a Remote Computer Beep

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

7.3. Create Melodies

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:

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.

8. Conclusion

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.