Created
March 8, 2026 22:14
-
-
Save CodeIter/fd5001db4e14e82b8b7ae9d5525c0895 to your computer and use it in GitHub Desktop.
ntfy.sh — simple shell wrapper
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/usr/bin/env bash | |
| set -euo pipefail | |
| usage() { | |
| cat <<EOF | |
| Usage: $0 [--help] [-t "title"] [-p "priority"] [-g "tags"] "topic" "message" | |
| Options: | |
| --help Show this help and exit | |
| -t TITLE Notification title | |
| -p PRIORITY Priority (0-5) | |
| -g TAGS Comma-separated tags | |
| Environment: | |
| NTFY_URL Base URL (default: https://ntfy.sh) | |
| NTFY_TOKEN Bearer token for protected topics (optional) | |
| Examples: | |
| $0 -t "Backup" -p 3 -g backup,server backups "Done" | |
| EOF | |
| } | |
| # defaults | |
| TITLE="" | |
| PRIORITY="" | |
| TAGS="" | |
| NTFY_URL="${NTFY_URL:-https://ntfy.sh}" | |
| AUTH_HEADER="" | |
| # parse args | |
| while (( $# )); do | |
| case "$1" in | |
| --help) | |
| usage | |
| exit 0 | |
| ;; | |
| -t) | |
| shift | |
| TITLE="$1" | |
| shift | |
| ;; | |
| -p) | |
| shift | |
| PRIORITY="$1" | |
| shift | |
| ;; | |
| -g) | |
| shift | |
| TAGS="$1" | |
| shift | |
| ;; | |
| --) shift; break ;; | |
| -*) | |
| echo "Unknown option: $1" >&2 | |
| usage >&2 | |
| exit 2 | |
| ;; | |
| *) | |
| break | |
| ;; | |
| esac | |
| done | |
| # require topic and message | |
| if [ "$#" -lt 2 ]; then | |
| echo "Error: topic and message required" >&2 | |
| usage >&2 | |
| exit 2 | |
| fi | |
| TOPIC="$1" | |
| shift | |
| MESSAGE="$*" | |
| # optional auth | |
| if [ -n "${NTFY_TOKEN:-}" ]; then | |
| AUTH_HEADER="Authorization: Bearer ${NTFY_TOKEN}" | |
| fi | |
| # build curl headers | |
| CURL_HEADERS=(-H "Content-Type: text/plain") | |
| [ -n "$TITLE" ] && CURL_HEADERS+=(-H "Title: $TITLE") | |
| [ -n "$PRIORITY" ] && CURL_HEADERS+=(-H "Priority: $PRIORITY") | |
| [ -n "$TAGS" ] && CURL_HEADERS+=(-H "Tags: $TAGS") | |
| [ -n "$AUTH_HEADER" ] && CURL_HEADERS+=(-H "$AUTH_HEADER") | |
| # send (POST) | |
| curl -sS "${CURL_HEADERS[@]}" -d "$MESSAGE" "${NTFY_URL%/}/$TOPIC" |
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
ntfy.sh — simple shell wrapper
Save as ntfy.sh and make executable (chmod +x ntfy.sh).
Notes: