Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

So I have a script that, when I give it two addresses, will search two HTML links:

echo http://maps.google.be/maps?saddr\=$1\&daddr\=$2 | sed 's/ /%/g'

I want to send this to wget and then save the output in a file called temp.html. I tried this, but ut doens't work. Can someone explain why and/or give me a solution please?

url = echo http://maps.google.be/maps?saddr\=$1\&daddr\=$2 |  sed 's/ /%/g'
wget $url
share|improve this question
For debugging something like this checking your variable values (by echo-ing them to the terminal) often gets you to the solution quickly. – kasterma Dec 13 '10 at 13:52
up vote 11 down vote accepted

You can use backticks (`) to evaluate a command and substitute in the command's output, like:

echo "Number of files in this directory: `ls | wc -l`"

In your case:

wget `echo http://maps.google.be/maps?saddr\=$1\&daddr\=$2 | sed 's/ /%/g'`
share|improve this answer
See Why $(...) is preferred over backticks. – jasonwryan Aug 10 '15 at 18:20

you're not actually executing your url line :

url="$(echo http://maps.google.be/maps?saddr\=$1\&daddr\=$2 | sed 's/ /%/g')"
wget $url
share|improve this answer

You could use "xargs". A trivial example:

ls -1 *.c | sort -n | xargs cat

You would have to take care that xargs doesn't split its stdin into two or more invocations of the comman ("cat" in the example above).

share|improve this answer

wget also accepts stdin with the - switch.

If you want to save the output in a file, use the -O switch.

echo http://maps.google.be/maps?saddr\=$1\&daddr\=$2 | sed 's/ /%/g' | wget -i- -O temp.html
share|improve this answer

It seems you could use a combination of the answers here. I'm guessing you are wanting to replace space chars with their escaped ascii values in the url. To do this, you need to replace them with "%20", not just "%". Here's a solution that should give you a complete answer:

$ wget `echo http://maps.google.be/maps\?saddr\=$1\&daddr\=$2 | sed -e 's/\ /\%20/g'` -q -O temp.html

The backticks indicate that the enclosed command should be interpreted first, and the result sent to wget. Notice I escaped the space and % chars in the sed command to prevent them from being misinterpreted. The -q option for wget prevents processing output from the command being printed to the screen (handy for scripting when you don't care about the in-work status) and the -O option specifies the output file. FYI, if you don't want to save the output to a file, but just view it in the terminal, use "-" instead of a filename to indicate stdout.

share|improve this answer

Your Answer


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