On August 26, 2018 In Blog, Tech, Tutorials

Sending Linux Variables to Discord Webhook from Linux Bash Shell Script

Sending Linux Variables to Discord Webhook from Linux Bash Shell Script

Discord provides an API where you can send messages/notifications via their webhook using curl . Which is all very helpful, except the code provided on their website only allows plain text, you can’t pass $variables onto it

Having started playing with Linux Bash scripts, it may seem natural or easy to the Linux veterans, definitely not for me

So after some research over at StackOverflow and self-test. Here is the code that will work when $variables are passed to it

Minor Update – 2020-04-22

Thanks and credits to skye#5254 for commenting in my Discord server that it’s better to use the “$@” to catch all variables including whitespaces

Latest Updated (better) script:

## format to parse to curl
## echo Sending message: $message

## discord webhook
curl -H "Content-Type: application/json" -X POST -d "{\"content\": $msg_content}" $url

More information on $* and $@ variables can be found here:


Previous version:


More on the code/script

The main aim is to output the curl command as follows:

curl -H "Content-Type: application/json" -X POST -d "{"content": "Your messages here"}" $url

Wrapped the messages with double quotes (escaped) to the $message variable, which is handled by $msg_content

Original code on Discord – note the single quotes ‘ ‘ wrapping of the JSON content

-d '{"username": "test", "content": "hello"}' $url

Also changed the ‘ ‘ to double quotes ” ” , so that Linux can return the data in $msg_content

How to use

Assuming you already know the basics of basics in Linux bash scripting, you will need to save that code above in a “.sh” file. Eg: discord-notify.sh

Anyhow, for users of all experience ranging from complete beginners to somewhat experienced Linux users

Here are the complete steps:

  1. SSH/Login/Open your Linux Terminal
  2. We will save the discord-notify.sh onto your user $HOME folder
    cd $HOME
    nano discord-notify.sh
  3. Paste in the code above, modify and paste your Discord webhook URL for $url variable
  4. Save the file by pressing Ctrl+X, answer the questions when asked
  5. Make discord-notify.sh executable
    chmod +x discord-notify.sh
  6. Prepare a variable for testing
    testdate="Text with whitespace and variable $(date +%T)"
  7. Test send a notification to your Discord Server/Channel
    ./discord-notify.sh "$testdate"

    Make sure you have the double quotes around your variable that you wish to send

And that’s it.



Note if you don’t double quote your $testdate variable, only “Text” get’s sent as shown on screenshot above

I’ll leave it to you on how you want to modify the code above to become a function to use in your scripts, or just call the discord-notify.sh script from another script


## This is "changetheworld-super-script.sh" Your almighty script
# Make a call to the discord-notify.sh script located in $HOME
$HOME/discord-notify.sh "$heal_the_world"

As there is no one “correct” way to program/script, appreciate it if you could share your version of code that worked for you, or share something even better!

Leave a comment if it helped 🙂

Notable Replies

  1. used the script, works for normal messages but when I write $ commands I get this
    {“message”: “Cannot send an empty message”, “code”: 50006}

  2. can you post your script ?

  3. #!/bin/bash
    rm -rf "${DISCORD}" && touch "${DISCORD}"
    echo "[test message] `date '+%T %A %d-%B,%Y'`  status done" >"${DISCORD}"
    # Send start message via Díscord
    if [ $DISCORD_WEBHOOK_URL != "null" ]; then
       message=$(cat "${DISCORD}")
       curl -H "Content-Type: application/json" -X POST -d "{\"username\": $USERNAME, \"avatar_url\": $IMAGE, \"content\": $msg_content}" $DISCORD_WEBHOOK_URL
       echo "${output}  failed"
    exit 0

    if you want to send more as a one_liner

    echo "[test message] \n `date '+%T %A %d-%B,%Y'`  \n status done" >"${DISCORD}"


  4. Ahhhhhhh I get you now

    So you want to output “$dk” to your channel as it is.

    We took it that you have a variable called $dk in Linux itself, not discord.

    In that case you would do this to your message content:

    your_text="white space and your '$dk' message"

    You need to wrap the $dk with Single Quotes(not Double-Quotes), to special-escape that whole string, which is $dk

    So if you only want to output “$dk” to your channel


    Have a try at it, @doob187 tested it and it worked :slight_smile:

    Let us know if it worked out :grin:

  5. Oh wait. There is one way, but it is not going to be easy. You will need to use Python and one of the libraries that act as you “typing” on the screen. Kinda like recording a macro command on any keyboard software

    1. Move mouse over to Discord channel (x,y)
    2. “type for you” , onto your channel
    3. Python press the “Enter/Return” key

    This is the only way if you want to be a “real” user

Add your comments/discussion at forums.jasonloong.com

15 more replies


Scroll Up