卷曲请求-如何转义Bash中无法识别的字符



我正在尝试使用curl更新密码,请参阅下面的示例代码:

# Define the required variables
keystorePassword="Password123"
userPassword="Password321"
user="username"
# Update the password using curl
response=$(curl -s -u "elastic:${keystorePassword}" 
-XPOST "http://localhost:9200/_xpack/security/user/${user}/_password" 
-d'{"password":"'"${userPassword}"'"}' -H "Content-Type: application/json")
echo "Output: $response"

这工作正常

但使用以下密码:

userPassword="Password!/5"

返回错误输出:"json_parse_exception", "reason":"Unrecognized character escape '5'", "status":400

有没有一种方法可以转义/处理userPassword变量中的特殊字符?

是转义符。它逃脱了后面的角色。由于后面的字符是5,因此它试图逃离5。因此,您需要转义才能将其用作字符,因此请使用\5

最新更新