Send SMS from Bash and Shell Scripts

To send an SMS from a shell script, call the curl-sms API with curl. Build the JSON with jq so quotes and line breaks in your text never break the request.

Example

#!/usr/bin/env bash
set -euo pipefail

# send_sms RECEIVER TEXT
send_sms() {
  jq -n --arg r "$1" --arg b "$2" '{receiver: $r, body: $b}' |
    curl --fail-with-body -sS https://curl-sms.com/api/sms/send \
      -H "Authorization: Bearer ${CURLSMS_KEY:?set CURLSMS_KEY}" \
      -H "Content-Type: application/json" \
      --data-binary @-
}

send_sms "+4915123456789" "Backup on $(hostname) finished"

Use a test key (export CURLSMS_KEY=cs_test_…) while you try it: every check runs, nothing is sent, nothing is charged.

What you get back

On success the API prints 202 JSON with id and status. On errors --fail-with-body prints the error JSON ({"error":{"code":…}}) and makes curl exit non-zero, so set -e stops the script. Extract fields with jq -r .id.

Tips

  • cron: cron runs with an almost empty environment. Set CURLSMS_KEY at the top of the crontab or in the script, and use full paths.
  • No duplicate alerts: add -H "Idempotency-Key: backup-$(date +%F)". A second run on the same day with the same text returns the first result instead of sending again.
  • Without jq: plain -d '{"receiver":"+49…","body":"Disk full"}' works for fixed texts; variables need escaping, which is why jq is safer.
  • Old curl: --fail-with-body needs curl 7.76+. On older systems use -f and lose the error body.

More: server and monitoring alerts with a complete health check script, or the quickstart.