在bash脚本中引用空间

问题描述

我有一个脚本,需要传递包含空格的字符串。

基本上,我想收集调用curl "https://my-api.com" -H'X-API-Key: API key here'

的错误返回

以下方法有效,但我确实希望避免使用eval。我敢肯定有一种更简洁的编写方式,但是找不到。

    CURL="curl -s --fail $URL -H\"X-API-Key:$API_KEY\""
    return $(eval "$CURL" >> /dev/null)

解决方法

这是Dynamically building a command in bash的副本,但这是您的代码的修补程序。

请花点时间阅读上级问题的答案。

# Build the curl command with its arguments in an array
curl_cmd=(curl -s --fail "$URL" -H "X-API-Key:$API_KEY")

# Execute the curl command with its arguments from the curl_cmd array
"${curl_cmd[@]}"

相关问答

依赖报错 idea导入项目后依赖报错,解决方案:https://blog....
错误1:代码生成器依赖和mybatis依赖冲突 启动项目时报错如下...
错误1:gradle项目控制台输出为乱码 # 解决方案:https://bl...
错误还原:在查询的过程中,传入的workType为0时,该条件不起...
报错如下,gcc版本太低 ^ server.c:5346:31: 错误:‘struct...