Making Your Internal Computer Speaker Beep From the Linux Command Line

This one is a quick one.  After looking around, this seems to be the easiest way to make a system beep to get your attention without having to install third-party packages.

Why?

Why would you want to do this?  This can be particularly useful in cases where you need to get the user’s attention in the context of a script, or where you want to be notified when a command has finished executing, or to give an audible indicator that a process is still happening (beeping in each iteration of a loop, for instance).

How

ASCII code 7 (which has the same representation in decimal, hex, and octal) is known as BEL or bell, and is the character representation of an audible beep on your computer’s internal speaker.   One representation of this character is an escaped ‘a’, that is, ‘\a’.  Sending this somewhere that can render that character (which is really just the audible tone, as it is one of the several non-printing characters) will make your computer beep.

This command will do it:

sudo sh -c “echo -e ‘\a’ > /dev/console”

This can of course be used on the command line itself, or within a script (so long as you have the proper permissions).

If you’re root, or otherwise have write permission to /dev/console, you should be able to do this as:

echo -e ‘\a’ > /dev/console

Dodging Permissions

Using a named pipe, as mentioned in my last post, we can dodge permissions a bit, but only in a context where you’re not worried about the potential of malicious code execution.

Step 1:

Create a named pipe somewhere:

mkfifo foo

Step 2:

Point the pipe at /dev/console with root permissions

sudo sh -c “tail -f foo > /dev/console” &

The & at the end backgrounds the task immediately so you can continue with other things.  To kill the process in the future, run the command ‘fg’ to bring it to the foreground, then use CTRL-C to kill the process.  Alternatively, use the ‘kill’ command to kill it after looking up its process id.

Step 3:

Dump the bell character to the named pipe as often as you like:

echo -e ‘\a’ > foo

Since the named pipe is writable by your normal user, you can do this as much as you like without having to use escalated privileges (like using sudo), just be aware that other normal users (or other processes running as you) may be able to write to this pipe (depending on its permissions) and execute code.

Leave a Reply