Bash脚本中定义curl --noproxy时单引号包含的正确语法
Bash脚本中CURL变量添加--noproxy '*'的正确语法
你遇到的问题是单引号在变量定义中的转义错误,以下是几种正确的写法:
方法一:用双引号包裹整个变量
双引号内部可以直接包含单引号,无需额外转义,仅需处理换行符的转义:CURL="curl --noproxy '*' --fail --max-time 10 --silent --output /dev/null --write-out '%{http_code}\\n'"验证时记得用双引号包裹变量输出,避免shell解析通配符:
echo "$CURL"输出结果:
curl --noproxy '*' --fail --max-time 10 --silent --output /dev/null --write-out '%{http_code}\n'方法二:单引号拼接实现嵌套单引号
若坚持用单引号包裹变量,可通过'\''在单引号字符串中插入单引号(逻辑为:结束当前单引号→插入转义的单引号→重新开启单引号):CURL='curl --noproxy '\''*'\'' --fail --max-time 10 --silent --output /dev/null --write-out '\''%{http_code}\\n'\'''更推荐的方法:使用数组存储参数
避免字符串变量带来的参数解析问题,用数组存储curl的每个参数,调用时能保证每个参数被正确识别:CURL=(curl --noproxy '*' --fail --max-time 10 --silent --output /dev/null --write-out '%{http_code}\n')调用方式:
"${CURL[@]}"
内容的提问来源于stack exchange,提问作者mickt
相关产品推荐
相关产品推荐

