Bash Script to Send Slack Notification Easily
curl to send a Slack notification by posting JSON data to your Slack webhook URL like curl -X POST -H 'Content-type: application/json' --data '{"text":"Your message"}' https://hooks.slack.com/services/your/webhook/url.Examples
How to Think About It
curl to post this data, which Slack then delivers to your chosen channel.Algorithm
Code
#!/bin/bash WEBHOOK_URL="https://hooks.slack.com/services/your/webhook/url" MESSAGE="Hello from Bash script!" curl -X POST -H 'Content-type: application/json' \ --data "{\"text\": \"$MESSAGE\"}" \ "$WEBHOOK_URL" echo "Notification sent to Slack channel with message: $MESSAGE"
Dry Run
Let's trace sending 'Hello from Bash script!' to Slack through the code
Set variables
WEBHOOK_URL='https://hooks.slack.com/services/your/webhook/url', MESSAGE='Hello from Bash script!'
Send POST request
curl sends JSON '{"text": "Hello from Bash script!"}' to the webhook URL
Print confirmation
Prints: Notification sent to Slack channel with message: Hello from Bash script!
| Step | Action | Value |
|---|---|---|
| 1 | Set MESSAGE | Hello from Bash script! |
| 2 | Send JSON payload | {"text": "Hello from Bash script!"} |
| 3 | Print output | Notification sent to Slack channel with message: Hello from Bash script! |
Why This Works
Step 1: Prepare webhook URL and message
The script stores your Slack webhook URL and the message text in variables to use them easily.
Step 2: Send JSON data with curl
Using curl with -X POST and -H 'Content-type: application/json' sends the message as JSON to Slack.
Step 3: Confirm message sent
The script prints a confirmation so you know the notification was triggered.
Alternative Approaches
#!/bin/bash MESSAGE="Hello from Slack CLI!" slack chat send --channel general --text "$MESSAGE" echo "Notification sent via Slack CLI"
#!/bin/bash python3 -c "import requests; requests.post('https://hooks.slack.com/services/your/webhook/url', json={'text': 'Hello from Python script!'})" echo "Notification sent via Python script"
Complexity: O(1) time, O(1) space
Time Complexity
The script runs a single HTTP POST request, so time is constant regardless of message size.
Space Complexity
Uses minimal memory for storing the message and JSON string; no extra data structures.
Which Approach is Fastest?
Direct curl usage is fastest and simplest; alternatives add overhead but offer more features.
| Approach | Time | Space | Best For |
|---|---|---|---|
| curl with webhook | O(1) | O(1) | Simple, quick notifications |
| Slack CLI tool | O(1) | O(1) | Frequent Slack interactions with auth |
| Python script | O(1) | O(1) | Complex payloads or integrations |