4

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.

1 Answer 1

8

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. ☺

1
  • Ahh, I feel silly now. Thanks for the detailed answer, as well as the additional information about the Bourne shell. :) Jan 23, 2014 at 20:00

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .