How to put text in quotes?

I have the following command which gives me value of Set-Cookie header:

curl --head http://www.stackoverflow.com | sed -n "/^Set-Cookie:/p" | cut -c 13-

Output:

prov=abed7528-7639-e2e3-39a0-361a6d3f7925; domain=.stackoverflow.com; expires=Fri, 01-Jan-2055 00:00:00 GMT; path=/; HttpOnly

I need this output in quotes, like this:

"prov=abed7528-7639-e2e3-39a0-361a6d3f7925; domain=.stackoverflow.com; expires=Fri, 01-Jan-2055 00:00:00 GMT; path=/; HttpOnly"

>Solution :

Using printf

printf '"%s"\n' "$(curl ...)"

Command substitution strips any trailing newlines, so the ending quote will be on the same line.

However, there is a trailing carriage return (network traffic generally uses \r\n line endings). Add this to the end of the pipeline

| tr -d '\r'
# or
| sed 's/\r$//'

Collapsing the pipeline into one sed command:

curl -s --head http://www.stackoverflow.com | sed -En '/^Set-Cookie:/ {
    s/^.{12}/"/
    s/\r$/"/
    p
    q
}'

Leave a Reply