How to Send an SMS from a Shell Script using cURL
Time to read: 3 minutes
Twilio is all about powering communication and doing it conveniently and fast in any language.
In this tutorial you’ll learn how to deliver a quick message or notification via SMS directly from your shell script. You’ll use the popular cURL command-line utility to send an SMS in an insanely fast manner. Ready? Let's get started!
Tutorial requirements
- A Twilio account. If you are new to Twilio click here to create a free account now and receive $10 credit when you upgrade to a paid account. You can review the features and limitations of a free Twilio account.
- cURL installed on your system. On Linux and MacOS it is likely installed by default. On Windows, download an installer from the cURL site.
- A smartphone with active service, to test the project
Buy a Twilio phone number
If you haven't done so already, purchase a Twilio phone number to send SMS from.
Log in to the Twilio Console, select Phone Numbers, and then click on the red plus sign to buy a Twilio number. Note that if you are using a free account you will be using your trial credit for this purchase.
In the “Buy a Number” page, select your country and check SMS in the capabilities field. If you’d like to request a number from your region, you can enter your area code in the “Number” field.
Click the “Search” button to see what numbers are available, and then click “Buy” for the number that you like from the results. After you confirm your purchase, click the “Close” button.
Configure Twilio credentials and phone numbers
To be able to send an SMS via Twilio, the script will need to have access to your Twilio account credentials to authenticate. Likewise, to send an SMS you need to have two phone numbers, the number of the sender, which is the number you bought in the previous section, and the number of the recipient, which can be your personal number.
The most secure way to define these configuration values is to set environment variables for them.
The Twilio credentials that you need are your “Account SID” and your “Auth Token”. You can find both on the dashboard of the Twilio Console:
In your terminal, define the following environment variables:
If you are following this tutorial on a Windows computer, use set
instead of export
to define your environment variables in the command prompt. For the phone numbers, use the E.164 format, which includes a plus sign and the country code.
If you want to learn more about environment variables, check out our how to set environment variables tutorial.
Send an SMS with cURL
With the four environment variables set as shown in the previous section, you can now send an SMS with a single-line command. If you are following this tutorial on a Unix or MacOS computer, here is how to do it:
For Windows, the syntax to insert variables in the command is slightly different, so use this version instead:
In just a moment, you will receive the SMS in your mobile phone!
Now you can insert this line in any shell scripts where you need to send notifications. You can also insert the phone number variables in your script, but be very careful with your Twilio credentials, as you don’t want them to end up in a public git repository. Check out the Store your Twilio Credentials Securely article in the documentation for more information on this.
If you are using a free Twilio account, keep in mind that the number that you use as a recipient must be verified before it can receive SMS. This requirement does not apply if you are using a paid account.
What's next for sending SMS with Twilio?
Congratulations on learning the one-liner that sends a quick SMS! Do you want to learn other original ways to send an SMS with Twilio? Here are some that you may like:
- How to Send SMS from a Google Spreadsheet
- How to send an SMS from React with Twilio
- Send SMS in Your Spring Boot App
- How to Send SMS in WordPress with a PHP Plugin
Miguel Grinberg is a Python Developer for Technical Content at Twilio. Reach out to him at mgrinberg [at] twilio [dot] com if you have a cool Python project you’d like to share on this blog!
Related Posts
Related Resources
Twilio Docs
From APIs to SDKs to sample apps
API reference documentation, SDKs, helper libraries, quickstarts, and tutorials for your language and platform.
Resource Center
The latest ebooks, industry reports, and webinars
Learn from customer engagement experts to improve your own communication.
Ahoy
Twilio's developer community hub
Best practices, code samples, and inspiration to build communications and digital engagement experiences.