0
0
Bash-scriptingHow-ToBeginner · 2 min read

Bash Script to Send Slack Notification Easily

Use a Bash script with 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

InputSend 'Hello World' to Slack
OutputNotification sent to Slack channel with message: Hello World
InputSend 'Build succeeded' notification
OutputNotification sent to Slack channel with message: Build succeeded
InputSend empty message
OutputNotification sent to Slack channel with message:
🧠

How to Think About It

To send a Slack notification from Bash, you need to send a JSON payload with your message text to a Slack webhook URL using an HTTP POST request. The script uses curl to post this data, which Slack then delivers to your chosen channel.
📐

Algorithm

1
Get the Slack webhook URL and message text as inputs.
2
Create a JSON payload with the message text.
3
Use curl to send an HTTP POST request with the JSON payload to the webhook URL.
4
Print a confirmation message after sending.
💻

Code

bash
#!/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"
Output
Notification sent to Slack channel with message: Hello from Bash script!
🔍

Dry Run

Let's trace sending 'Hello from Bash script!' to Slack through the code

1

Set variables

WEBHOOK_URL='https://hooks.slack.com/services/your/webhook/url', MESSAGE='Hello from Bash script!'

2

Send POST request

curl sends JSON '{"text": "Hello from Bash script!"}' to the webhook URL

3

Print confirmation

Prints: Notification sent to Slack channel with message: Hello from Bash script!

StepActionValue
1Set MESSAGEHello from Bash script!
2Send JSON payload{"text": "Hello from Bash script!"}
3Print outputNotification 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

Using Slack CLI tool
bash
#!/bin/bash
MESSAGE="Hello from Slack CLI!"
slack chat send --channel general --text "$MESSAGE"
echo "Notification sent via Slack CLI"
Requires installing Slack CLI and authentication; easier for frequent Slack interactions.
Using Python script called from Bash
bash
#!/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"
Uses Python requests for more complex payloads; requires Python installed.

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.

ApproachTimeSpaceBest For
curl with webhookO(1)O(1)Simple, quick notifications
Slack CLI toolO(1)O(1)Frequent Slack interactions with auth
Python scriptO(1)O(1)Complex payloads or integrations
💡
Always keep your Slack webhook URL secret to prevent unauthorized messages.
⚠️
Forgetting to escape quotes inside the JSON payload causes syntax errors in the curl command.