Home > Back-end >  How to escape double quotes in below bash script that is being used to send messages on discord usin
How to escape double quotes in below bash script that is being used to send messages on discord usin

Time:02-18

I'm using following script to send messages to discord using webhook:

#!/bin/bash
webhookurl="https://discord.com/..."
messagearray=($(curl -s -i https://www.google.com/ | awk 'NR==12'))
message=`echo ${messagearray[@]} | tr -cd "0-9A-Za-z .=_,:\"()-[]{}\\'/'"`;
curl -H "Content-Type: application/json" -d '{"content": "'"$message"'"}' "$webhookurl"

The output in terminal is: alt-svc: h3=":443"; ma=2592000,h3-29=":443"; ma=2592000,h3-Q050=":443"; ma=2592000,h3-Q046=":443"; ma=2592000,h3-Q043=":443"; ma=2592000,quic=":443"; ma=2592000; v="46,43"

But in discord it sends without double quotes like this: alt-svc: h3=:443; ma=2592000,h3-29=:443; ma=2592000,h3-Q050=:443; ma=2592000,h3-Q046=:443; ma=2592000,h3-Q043=:443; ma=2592000,quic=:443; ma=2592000; v=46,43

I've used \" to escape double quotes but this doesn't seems to be working. What's the possible solution?

CodePudding user response:

You do not "escape quotes". To handle JSON you use JSON libraries and tools. Like jq.

message="anything you want here"
data=$(jq -n --arg message "$message" '.content = $message')
curl -d "$data" ...

Check your script with shellcheck.

  • Related