問題:shell腳本寫了一個簡單的curl執行,但是執行不起來結果
從postman里生成的命令如下
curl -X PUT \ https://t-im-prod.hillinsight.tech/v1/accounts/deleteAccount \ -H 'Authorization: ' \ -H 'CustomHeader: xx' \ -H 'Postman-Token: 937e5aae-9e94-4221-bd30-d6a4eabf6111' \ -H 'cache-control: no-cache'
然后復制到shell文件里
#!/bin/bash if [ -z "$1" ]; then echo "please input param" exit -1 fi
#簡單的這么寫即可生效
#curl -X GET www.baidu.com
cmd=`curl -X PUT \ https://xx/xxx \ -H 'Authorization: xx' \ -H 'CustomHeader: $1' -s` echo "$cmd"
執行了,但是沒完全執行。沒生效,后來查詢發現shell中單引號 ' 和雙引號 "有區別,修改后生效
#!/bin/bash if [ -z "$1" ]; then echo "please input param" exit -1 fi #-s 防止輸出這些信息 # % Total % Received % Xferd Average Speed Time Time Time Current # Dload Upload Total Spent Left Speed # 0 0 0 0 0 0 0 0 --:--:-- --:--:-- --:--:-- 0 cmd=`curl -X PUT \ https://xx/xxx \ -H "Authorization: xx" \ -H "CustomHeader: $1" -s` echo "$cmd"
結論:shell中單引號 ' 和雙引號 "有區別
單引號告訴shell忽略所有特殊字符,而雙引號只要求忽略大多數,具體說,括在雙引號中的三種特殊字符不被忽略:$,\,` ,即雙引號會解釋字符串的特別意思,而單引號直接使用字符串.
例如:
n=3 echo '$n' #輸出結果:$n echo "$n" #輸出結果:3