How can I use a variable in curl call within bash script

前端 未结 2 470
借酒劲吻你
借酒劲吻你 2020-12-20 16:21

I have this simple task and I\'ve spent a few hours already trying to figure out how can I use a variable inside a curl call within my bash script:

message=\         


        
2条回答
  •  予麋鹿
    予麋鹿 (楼主)
    2020-12-20 16:53

    Variables are not expanded within single-quotes. Rewrite using double-quotes:

    curl -X POST -H 'Content-type: application/json' --data "{\"text\": \"${message}\"}"
    

    Just remember that double-quotes within double-quotes have to be escaped.

    Another variation could be:

    curl -X POST -H 'Content-type: application/json' --data '{"text": "'"${message}"'"}'
    

    This one breaks out of the single quotes, encloses ${message} within double-quotes to prevent word splitting, and then finishes with another single-quoted string. That is:

    ... '{"text": "'"${message}"'"}'
        ^^^^^^^^^^^^
        single-quoted string
    
    
    ... '{"text": "'"${message}"'"}'
                    ^^^^^^^^^^^^
                    double-quoted string
    
    
    ... '{"text": "'"${message}"'"}'
                                ^^^^
                                single-quoted string
    

提交回复
热议问题