Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

While searching for a simple bash function that echoes my external IP address, I came across this post, which had the following example:

function ext-ip () { curl http://ipecho.net/plain; echo; }

I'm wondering how echo works in this context; neither the curl nor the echo manpages were very helpful. Why does it echo the response body of the previous curl command? And is there any advantage to doing in this way as opposed to, e.g. echo "$(curl -s http://ipecho.net/plain)"? I'm only asking because the syntax of the latter seems more transparent/straightforward.

share|improve this question

1 Answer 1

up vote 5 down vote accepted

Why does it echo the response body of the previous curl command?

A classic loaded question.

It doesn't do any such thing. The curl command outputs an IP address without a trailing newline. The echo command supplies the newline. It's that simple.

Whether

echo "$(curl -s http://ipecho.net/plain)"

is more straightforward than

curl http://ipecho.net/plain ; echo

is a matter of taste to some extent. It's worth noting, however, that $() is not available in the Bourne (not to be confused with the Bourne Again) shell nor in the C shells, whereas pretty much all shells can execute two plain commands in sequence separated by a semi-colon. That even includes csh and fish. ☺

share|improve this answer
    
Ahh, I feel silly now. Thanks for the detailed answer, as well as the additional information about the Bourne shell. :) –  ysim Jan 23 at 20:00

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.