Search code examples
bashcurlgitlablibcurlgitlab-api

How to use \r and \n (new line character) with Curl command?


I use following bash script line to update the description of GitLab group which use curl. But it prints in the description

Test_1\r\nTest_2\r\nTest3 

I want this

Test_1
Test_2
Test_3

Bash script command

curl --request PUT --header "PRIVATE-TOKEN: $GIT_TOKEN" "$GIT_API/groups/1079?description=Test_1\r\nTest_2\r\nTest3"

Solution

  • Use C-Style strings that can contain decoded escaped characters like:

    $'Test_1\r\nTest_2\r\nTest3'

    curl \
      --request PUT \
      --header "PRIVATE-TOKEN: $GIT_TOKEN" \
      "$GIT_API/groups/1079?description="$'Test_1\r\nTest_2\r\nTest3'
    

    Eventually:

    group_id=1079
    group_description=$'Test_1\r\nTest_2\r\nTest3'
    
    curl \
      --request PUT \
      --header "PRIVATE-TOKEN: $GIT_TOKEN" \
      "$GIT_API/groups/$group_id?description=$group_description"