Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

Trying to capture and display the output of curl was driving me nuts. I finally found a workaround, but I don't understand why it is needed.

Here's a script that fails:

#!/bin/bash
function echo_output() {
    echo $@
}

CURL_OUTPUT=$(curl -sL --head www.google.com/foobar)
echo_output "$CURL_OUTPUT"

Here's a script that works: (escaping the carriage returns)

#!/bin/bash
function echo_output() {
    OUT="${@//[$'\r']/\r}"
    echo $OUT
}

CURL_OUTPUT=$(curl -sL --head www.google.com/foobar)
echo_output "$CURL_OUTPUT"

What gives? What is special about newlines and "$@"? Thanks for the help.

share|improve this question
    
It's not $@ that is "special", but echo. Replacing echo with printf probably simplifies things for you. –  dawud May 5 '13 at 19:09
    
Also, check the difference between quoting $@ and don't quoting it,here and here –  dawud May 5 '13 at 19:19
add comment

1 Answer

up vote 1 down vote accepted

I do not think there is anything wrong with $@.

But in the first case in order cleanly output that variable as a parameter you assigned to curl with output having newlines, in your function echo_output() you need to quote the "$@", in this case "echo"(which in turn does only printing its positional arguments into standard output and nothing more) will split the words -> arguments(parameters) correctly. As Dawud mentioned it is better to use printf in this cases.

Again in your case:

function echo_output() {
echo "$@"
}

will do it.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.