Skip to content
 

Wget and URLs with Ampersands

I just spent more time than I should have trying to figure out why wget was not retrieving web pages correctly. I’ve been using wget for ages with no problems. It turns out the ampersand & in the URL was behaving badly. The solution is obvious, once I found it buried deep in a discussion at RedHat.com. Enclose the URL in single quotes. Works like a charm.

9 Comments

  1. Mark says:

    I agree, this was not clearly mentioned in the manual – that I saw anyway and I followed some other poor suggestions about encoding or double quotes before reading this.

    Single quotes and all is good.

  2. Mark says:

    Oh my god thank you so much!!! I Spent AGES trying to get this to work (using encoding and double quotes etc) and I can’t believe I didn’t try single quotes, such an obvious solution, why isn’t it mentioned in the manual?

  3. Jesse Dyer says:

    Granted this is an old, but for those who come here via Google; the reason that this isn’t mentioned in the manual is that this is not a wget problem; this is a bash problem. Bash interprets ampersands as a control operator. Everything before, and separately after the ampersand gets passed as a single standalone command.

  4. Mark says:

    Informative post, i made sure that I followed suggestions about encoding or double quotes and it really worked well for me. Thanks 😀

  5. Charles Hope says:

    You can also use a backslash before the ampersand. This is useful if the URL contains a variable that you’d need substituted, such as

    http://server“$n”.company.com/control?active=true\&password=password

  6. Deborah Simons says:

    Very useful post! Charles, you got it right with the backslash before the ampersand. Thanks for sharing the info.

  7. Harry Klitza says:

    Spot on! This is exactly what I was looking for. The backslash before the ampersand works a treat.

    Harry

  8. paritosh says:

    i know this is very old threads but …
    Thanks.
    Just recently moved to perl/bash for some quick cooking and spent almost 2 hours looking wget downloading 2 files when using & char.
    you saved the day 🙂
    By the way \& didn’t worked for me

  9. Alex says:

    Here’s another technique:

    URL=”http://example.com/some/query?ENTRY=something&this&that”; wget “$URL”