You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

自定义字符串包裹符号定义与Slack curl请求payload变量解析替代方案咨询

Hey there! Let's work through your two needs—creating custom string wrapping symbols and fixing that annoying Slack curl payload variable parsing issue without escaping every double quote. Here are practical, straightforward solutions:

Solutions for Slack curl Payload & Custom String Wrapping

1. Fixing Slack curl Payload Variable Parsing (No Escaped Double Quotes)

Single quotes in bash prevent variable expansion, which is why your payload variables aren't being parsed. Instead of escaping all double quotes, try these alternatives:

a. Use a Bash Here-Doc with curl --data @-

This is my go-to for clean, readable payloads. The here-doc lets you write JSON naturally, with variables expanding normally, no escaping required:

SLACK_WEBHOOK="https://hooks.slack.com/services/YOUR/WHOOK/URL"
MESSAGE="Hey team, the script ran successfully!"
BOT_NAME="AutomationBot"

curl -X POST -H "Content-Type: application/json" "$SLACK_WEBHOOK" --data @- <<EOF
{
  "text": "$MESSAGE",
  "username": "$BOT_NAME",
  "icon_emoji": ":rocket:"
}
EOF

The @- tells curl to read the payload from standard input, and the <<EOF/EOF block lets you write valid JSON with variables that bash will resolve automatically.

b. Generate Valid JSON with jq (Safe for Special Characters)

If your message has special characters (quotes, brackets, etc.), jq will handle JSON escaping for you while pulling in variables from your environment:

# Export variables so jq can access them
export SLACK_MSG="Alert: Server load is *high*! Check it out ASAP."
export SLACK_USER="StatusBot"

curl -X POST -H "Content-Type: application/json" "$SLACK_WEBHOOK" \
  --data "$(jq -n '{text: env.SLACK_MSG, username: env.SLACK_USER}')"

jq -n creates a new JSON object, and env.VAR_NAME pulls in your environment variables. No manual escaping needed, and you avoid invalid JSON errors.

c. Mix Quote Types for Partial Expansion

Split your payload into static parts (wrapped in single quotes) and variable parts (wrapped in double quotes), then concatenate them:

ALERT="Database backup completed at $(date)"
curl -X POST -H "Content-Type: application/json" "$SLACK_WEBHOOK" \
  --data '{"text": "'"$ALERT"'", "channel": "#operations"}'

Breakdown:

  • '{"text": "' = static JSON part (single quotes prevent expansion)
  • "$ALERT" = variable part (double quotes let bash expand it)
  • '", "channel": "#operations"}' = another static part

2. Defining Custom String Wrapping Symbols

If you want to use something other than single/double quotes to wrap strings, here are two easy ways:

a. Custom Wrapping Function

Create a simple bash function to wrap any string in your chosen symbols (e.g., <<>>):

wrap_with_custom() {
  # Takes a string as input and wraps it in <<>>
  echo "<<$1>>"
}

# Example usage
MY_CONTENT="This is my wrapped text"
WRAPPED=$(wrap_with_custom "$MY_CONTENT")
echo $WRAPPED  # Output: <<This is my wrapped text>>

You can tweak the function to use any symbols you want—just change the echo "<<$1>>" line to your preferred delimiters.

b. Here-Doc with Custom Delimiters

Use a custom delimiter in a here-doc to "wrap" multi-line or complex content. This is great for blocks of text that need variable expansion:

# Define your custom delimiter (can be any unique string)
read -r -d '' WRAPPED_CONTENT <<CUSTOM_WRAP
This is my custom-wrapped content.
It supports newlines, variables: $MESSAGE,
and even special chars like " ' [ ] { }
CUSTOM_WRAP

echo "$WRAPPED_CONTENT"

If you don't want variables to be expanded (keep them literal), just quote the delimiter: <<'CUSTOM_WRAP'.


内容的提问来源于stack exchange,提问作者Dr. Gianluigi Zane Zanettini

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.19 08:44:47