107

I have a script running in the background and sends me an alert every few minutes. I want the alert to be in the form of a beep.

Question: How can I play a beep in mac terminal?

rk.
  • 1,195
  • 2
  • 8
  • 7

6 Answers6

186

printf \\a and osascript -e beep play the default alert sound, but they are silent if the alert volume is set to zero. printf \\a is also silent if an audible bell is disabled.

You could also use afplay or say:

afplay /System/Library/Sounds/Funk.aiff
say done

There are more sound effect files in /System/Library/PrivateFrameworks/ScreenReader.framework/Versions/A/Resources/Sounds/.

Ahmed Ashour
  • 2,350
  • 2
  • 14
  • 21
Lri
  • 40,894
  • 7
  • 119
  • 157
  • I was using say till now, afplay did the trick. Thanks! – rk. May 22 '13 at 22:19
  • 3
    Sweet! You can use `say -v ?` (in Yosemite, at least) to get a list of voices installed -- I had several! Here's a little script to say what you want in every available voice: `for i in $(say -v \? | awk '{print $1;}'); do echo $i; say -v $i "Build terminated\!"; done` – scorpiodawg Jan 27 '15 at 17:25
  • 2
    In macOS Catalina, I had to use `say -v '?'` with single quotes because of zsh. – Funktional Nov 12 '20 at 01:51
23

The simplest way is the use a bell echo -e "\a"

demure
  • 6,080
  • 1
  • 27
  • 29
  • 2
    Didn't work for me. Do I need a package? – rk. May 22 '13 at 21:37
  • What version of OS X are you on? Also, check your terminal emulator's settings, and make sure you don't have bell disabled. – demure May 22 '13 at 21:38
  • Ah! Terminal sounds were not enabled. Also, is there a decent bell/alert compared to the dull thud sound this command makes? – rk. May 22 '13 at 21:41
  • I use iTerm2 myself, which uses growl (so bells go to growl), via growl I add another sound to iTerm2 alerts. Yeah, kind of round-about. – demure May 22 '13 at 21:45
  • Ohk, I will use that setup as a last resort ;) – rk. May 22 '13 at 21:50
  • You can change the sound played in System Preferences. The sound used by terminal is just the system wide alert sound which you can find in the Sound prefs. – Tonny May 22 '13 at 21:52
  • @Tonny quite valid, and I should have recalled to mention that. I don't use that method as I wish to tell the difference between the command line and the rest of the system beeping at me. – demure May 22 '13 at 21:57
  • @demure Ideally me too, but I run so few scripts (on OSX) that I never bothered to setup something up like that. I just enable "Visible bell" to get that little extra feedback. – Tonny May 22 '13 at 22:02
  • discovered that echo ^G and tput bel have the same effect. – rk. May 22 '13 at 22:02
  • Not sure why this warranted a down vote :/ – demure Sep 19 '13 at 20:11
  • What does the `-e` option do? It is not listed in the man pages – Startec May 27 '15 at 20:48
  • A way to make a command-line alias for one (without escape sequences) is: alias beep='tput bel' – Jeff Clayton Nov 21 '15 at 14:31
  • `-e` is a non-POSIX extension supported by some shells, e.g., `bash`. Instead, prefer to use the portable `printf` command, e.g., `printf '\a'`. Still, the most portable solution is `tput bel` because the terminal may use something other than or in addition to Control-G. – Chris Page Aug 01 '22 at 18:44
6

Another way is to echo ^G. But you don't literally type the ^G. Instead, type ctrl+v, ctrl+g, which will appear as echo ^G.

wisbucky
  • 2,928
  • 31
  • 29
5

tput bel works in most shells.

From this answer on a related Stack Overflow question:

Kenny Evitt
  • 395
  • 4
  • 16
5

Hear and pick

ls /System/Library/Sounds/ | awk '{print $1}' | while read sound; do printf "using $sound...\n"; afplay /System/Library/Sounds/$sound; sleep 0.5; done

ls /System/Library/PrivateFrameworks/ScreenReader.framework/Versions/A/Resources/Sounds/ | awk '{print $1}' | while read sound; do printf "using $sound...\n"; afplay /System/Library/PrivateFrameworks/ScreenReader.framework/Versions/A/Resources/Sounds/$sound; sleep 0.5; done
0

There's one more way that can be useful in cases like, let's say you forgot to set the alert and in the middle of the process, and on Iterm. Simply Cmd + Option + A while in terminal screen lets you have a notification in the end of the process.

zx_
  • 11