Building command strings using variables with various quote levels and spaces

前端 未结 3 2033
暗喜
暗喜 2021-01-14 07:34

I have a script that runs curl. I want to be able to optionally add a -H parameter, if a string isn\'t empty. What\'s complex is the levels of qu

3条回答
  •  陌清茗
    陌清茗 (楼主)
    2021-01-14 08:18

    The key to making this work is to use an array.

    caption="Test Caption"
    
    if [[ $caption ]]; then 
        CAPT=(-H "X-Caption: $caption")
    fi
    
    curl -A "$UA" -H "Content-MD5: $MD5" -H "X-SessionID: $SID" -H "X-Version: 1"  "${CAPT[@]}" "http://upload.example.com/$FN"
    

提交回复
热议问题