One of the first fun tricks I picked up when I was first learning programming was how to use the
say command in my terminal. It can be great for making friends laugh and playing around. But did you know that it does a lot more than just speak? Here’s a quick rundown of some of say‘s functionality and a couple of ideas of what you can do with it!
The Functionality of Say
How to make your computer talk:
- Make sure your sound is on. Go ahead, turn it up.
- Open the terminal.
sayfollowed by a string of words your want your computer to speak. (e.g.,
say hello world)
That’s it! That’s the basics of the say command, and the part that many people are familiar with. It also does so much more!
Did you know that the say command can:
- Use different voices?
- Read from a file?
- Output to an audio file?
- Speak slower or faster?
- Highlight words while they’re being spoken?
Yep, it can do all of that. Let’s go through how those work!
Using Different Voices
To list the available voices,
say -v ?. There are quite a few! To use one, type:
say -v Fred hello world!
Reading from a text file
The say command allows you to specify a file to read. To try this out, create a file called
puffin.txt. Let’s type some text in there:
Oh, there once was a Puffin just the shape of a muffin, and he lived on an island in the bright blue sea!
Now in your terminal, type
say -f puffin.txt. Note that you need to specify the file path to the file you’re reading from. Way cool!
Outputting to an audio file
To write to a file, use the
-o option, passing it in the file name to write to. The default file type is
.aiff, or Audio Interchange File Format. Let’s trying reading our text file we made above into the audio file! Do that with this command:
say -f puffin.txt -o puffin.aiff
Now, go find that file and play it. You can listen to poetry all day long!
Change how quickly words are spoken
This one is simple. Add the
-r option to your command, with the value being the number of words spoken per minute. Go ahead, try these two:
say -r 100 I am talking slowly say -r 400 I am talking quickly
Highlight words while they’re being spoken
Highlight words as they’re spoken with a color.
say --interactive=/blue this is blue; say --interactive=/red this is red
Let’s get creative with it!
Dubstep in your command line:
yes 'wub' | xargs say
ctrl+c to stop it.
How about a little Whitney Houston in your command line:
say -v Vicki -r 100 "and i e i e i ei will alll ways love you oo oo uu ah"
How can you use say in your programming?
Output messages to yourself!
Do you have any long-running terminal processes that you’d like to have an alert for when they finish? Say is just what you need!
Uploading something to heroku? Installing node modules? Chain say on there for an alert!
npm install && say "done installing!"
I once worked on a project with a very large, slow RSpec test suite. Rather than sit and watch the test suite run, or wander off and forget that I was running it at all, I used an alert to let me know when it was ready. There’s a great little gem by former QL-er, Brent Ertz that handles it for you: Holla.
There’s a lot of ways to say with your terminal! To read the full documentation of the say command, type
man say in your terminal. Do you have fun tricks or innovative uses for it? Share them below!