0

I'm getting a slightly different result when I run curl as a command than when I put its output in a variable and then read said variable.

1

x=`curl http://www.movies.com/rss-feeds/top-ten-box-office-rss 2> /dev/null`; echo $x

2

curl http://www.movies.com/rss-feeds/top-ten-box-office-rss 2> /dev/null

The differences between them are that when I do 2, I get everything and it is all formatted, and that, when I use option 1, besides being all one block, I miss all of the following (for each movie):

<pubDate>Tue, 04 Mar 2014 08:00:00 GMT</pubDate>
      <source url="http://www.movies.com">Movies.com Top 10 Box Office</source>
    </item>
    <item>
      <title><![CDATA[10. Ride Along - $2.0M]]></title>

I saw a similar question here (In ShellScript Assign Variable Based on Curl Output), but when I tried the proposed solution I get the same difference. Reading (and trying) flags doesn't help.

Apologies for the really beginner question.

2 Answers 2

2

Use:

echo "$x"

to prevent word-splitting of $x.

In general, you should quote variables unless you know that they don't contain whitespace or wildcards, or you explicitly want those characters to be processed.

Sign up to request clarification or add additional context in comments.

Comments

1

With echo $x (version 1), the contents of x are placed on the command line as arguments to echo and interpreted according to shell rules, and then echoed to the terminal. Use echo "$x" to prevent this.

With that one change, the two methods will produce output that is differs only by one character: the final newline. That character is removed by the shell when processing backticks. man bash documents this:

Bash performs the expansion by executing command and replacing the command substitution with the standard output of the command, with any trailing newlines deleted. Embedded newlines are not deleted, but they may be removed during word splitting.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.