Building command strings using variables with various quote levels and spaces

前端 未结 3 1995
暗喜
暗喜 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:15

    If you only need to know whether or not the caption is there, you can interpolate it when it needs to be there.

    caption="Test Caption"
    NOCAPT="yeah, sort of, that would be nice"
    
    if [ "${caption}" != "" ]; then 
        unset NOCAPT
    fi
    
    curl ${NOCAPT--H "X-Caption: ${caption}"} -A "$UA" ...
    

    To recap, the syntax ${var-value} produces value if var is unset.

提交回复
热议问题