1. Introduction

In Linux, it’s possible to send Short Message Service (SMS) either directly from the terminal using SMS gateways or by employing tools that support sending SMS through the command line. This approach is common for automation, notifications, or system monitoring.

In this tutorial, we’ll learn how to send SMS using the command-line interface (CLI). First, we’ll look at the Textbelt tool. Next, we’ll explore how to send an SMS with TextMagic. Finally, we’ll get to see how to send SMS using Twilio.

2. Using Textbelt

Textbelt offers one of the most straightforward ways to send an SMS message from the command line. It’s a service that provides an application programming interface (API) for sending SMS messages.

To illustrate, let’s send an SMS using Textbelt:

$ curl -X POST https://textbelt.com/text \
       --data-urlencode phone='8161581281' \
       --data-urlencode message='Welcome to winter' \
       -d key=textbelt

{"success": true, "quotaRemaining": 40, "textId": 1012}

Now, let’s break down the command:

  • curl -X POST https://textbelt.com/text sends a POST request to the Textbelt API
  • –data-urlencode phone=’8161581281′ sets the phone number that’ll receive the SMS
  • –data-urlencode message=’Welcome to winter’ encodes the message we want to send
  • -d key=textbelt specifies the API key for sending the SMS

Notably, using key=textbelt provides one free text per day.

Further, Textbelt offers free and paid versions of their SMS service. Also, we can interact with the API using different programming languages.

3. Using TextMagic

Alternatively, we can send an SMS from the command line using the TextMagic SMS gateway. The TextMagic application programming interface (API) provides a communication link that enables us to send messages using its service.

There are three main steps to send an SMS using TextMagic:

  1. download the latest version of the TextMagic Bash Wrapper from GitHub
  2. get and set up the API credentials
  3. move the script file, tm.sh, into any directory or PATH for global access

So, after downloading the wrapper, we create the API credentials (account username and API access token) by also creating an account and logging in to TextMagic.

Once the API credentials are set up, let’s edit the configuration section of the TextMagic script file, tm.sh, to include the credentials:

readonly BASE_URL="https://rest.textmagic.com/api/v2"       # API base URL
readonly USERNAME="baeldung"                                # Account username
readonly TOKEN="US22f92235dd153183ca21e85276055a93"         # API access token

Now, let’s send an SMS through the shell using TextMagic:

$ tm.sh send --text="Welcome To Ohio" --phones=14603212130

Here, tm.sh uses the send subcommand to send an SMS with the message Welcome to Ohio to the specified phone number.

Notably, the shell wrapper for the TextMagic API only works in Bash versions 3 or newer. In addition, the receiver’s phone number we use on TextMaigc must be in the E.164 format. In other words, it doesn’t accept local number formats.

4. Using Twilio

Twilio provides a programmable messaging API for sending SMS messages directly from Bash.

4.1. Get and Set Twilio Credential Variables

To use Twilio, we can sign up for a free account and log in on Twilio. Also, we have to verify our phone number and email to activate the account.

Then, we use the USER SID and AUTH TOKEN from Twilio to set up  the respective shell variables:

$ account_sid="US22f92235dd153183ca21e85276055a93"
$ auth_token="MY_AUTH_TOKEN"

Notably, these variables are critical for the next steps. Again, we can get the values assigned to these variables from our Twilio account.

4.2. Finding and Purchasing a Twilio Phone Number

Further, having set up our USER SID and AUTH TOKEN, we can query the Twilio REST API for programmable phone numbers:

$ available_number= $(curl -X GET \
    "https://api.twilio.com/2010-04-01/Accounts/${account_sid}/AvailablePhoneNumbers/NG/Local"  \
    -u "${account_sid}:${auth_token}" | \
    sed -n "/PhoneNumber/{s/.*<PhoneNumber>//;s/<\/PhoneNumber.*//;p;}") \
    && echo $available_number

Again, let’s break down the command:

  • curl -X GET “https://api.twilio.com/… uses curl and the HTTP GET to query Twilio for an available number
  • 2010-04-01 in the API URL represents the API version
  • -u signifies that we will be providing account credentials by HTTP Basic Auth
  • “${account_sid}:${auth_token}” substitutes the variable names we set initially into the command and passed to the next using |
  • sed -n “/PhoneNumber/{s/.*<PhoneNumber>//;s/<\/PhoneNumber.*//;p;}” extracts just the first occurrence of the <PhoneNumber> tags from Twilio’s response

Finally, the command sets the available_number variable to the first number Twilio returns, and echo prints it.

Once we successfully find an available number, we can purchase it for use. To illustrate, let’s see a command that purchases the phone number we found and add it to our account:

$ curl -X POST -d "PhoneNumber=${available_number}" \
    "https://api.twilio.com/2010-04-01/Accounts/${account_sid}/IncomingPhoneNumbers" \
    -u "${account_sid}:${auth_token}

Finally,  we should have our new ready-to-use Twilio phone number after executing this command. However, if this step is unsuccessful, we can re-run the process of finding a number.

4.3. Sending an SMS With Twilio in Bash

Now, we can employ a single command to send an SMS using Twilio in the CLI. Notably, we have to provide the recipient’s number in the recipient variable. Also, the content of the message is written and assigned to the Body parameter:

$ receiver="+146730202887"
curl -X POST -d "Body=Hello, Christmas is here." \
    -d "From=${available_number}" -d "To=${recipient}" \
    "https://api.twilio.com/2010-04-01/Accounts/${account_sid}/Messages" \
    -u "${account_sid}:${auth_token}"

In essence, this command requires three parameters, Body, From, and To, to send a POST request to the Messages endpoint. With all the required information present, the command sends the message to the provided phone number.

5. Conclusion

In this article, we learned how to send SMS from the command-line interface using three methods.

Initially, we briefly saw how to send an SMS using Textbelt. Then, we discussed how to use an alternative, TextMagic. Finally, we set up a Twilio account and demonstrated how to send an SMS through the command line using Twilio.