Bash variable gets butchered when being inserted into cURL command

前端 未结 1 570
忘了有多久
忘了有多久 2020-12-21 09:26

The first part of the script looks like:

curl -k -X POST -H \"Content-Type: application/x-www-form-urlencoded\" -H \"Cache-Control: no-cache         


        
相关标签:
1条回答
  • 2020-12-21 10:06

    First: Your token currently has literal CRs in it -- the apparent butchering is caused by those CRs being printed in your echo output, moving the cursor back to the beginning of the line so parts of your token get overwritten with later content. To eliminate them, you can either convert the file into UNIX text format (and ensure that there aren't CRs in any of the commands generating that file), or you can use:

    token=$(<token.txt)    # read token.txt into "token" variable
    token=${token//$'\r'/} # strip CRs from token variable
    

    If you want to pass the Authorization header to curl as a single word, don't end and reinitialize your quotes; keep that single argument inside one set of syntactic quotes.

    -H "Authorization: AR-JWT $token"
    

    Finally, make sure your script itself is in UNIX format -- that your editor isn't saving it with DOS newlines.

    0 讨论(0)
提交回复
热议问题