Most probably, "foo" and "bar" were derived from "foobar," which in turn had its origins in the military slang acronym FUBAR. The most common rendition is "Fucked Up Beyond All Recognition" (Several variant renditions exist, with, for example, the words "Fouled", "Any Repair", "Any Redemption", or "All Reason" used instead of the versions above.)
FUBAR may have had its origins in the German word Furchtbar, meaning frightful, or terrible. It is pronounced with a soft cht, and probably made the transition during World War II. "Foo" had been popularized in American culture, appearing in a 1938 Warner Bros. Daffy Duck cartoon and the comic strip Smokey Stover. Electronics engineers say that snafu and fubar were used before the war by repairmen sent out to repair phone booths. They had to report the situation at arrival to the scene, often on a very bad line, so they developed these acronyms to make themselves understood.
It is also possible that foobar is a phonological interpretation of the first letters of the Runic alphabet. Like Qwerty and Abcde, this expression might have attracted various computer programmers. In a museum at Aarhus, a large wooden bar with the runic enscription f u þ a r, where fu is pronounced like foo. However, the letter þ is actually pronounced like an unvoiced th, not a b (hence the name Futhark for the Runic alphabet).
One of the most popular hacker/demo groups on the Commodore 64 scene in the mid 1980s was FBR - "Fucked Beyond Repair". Except in military and computer science/hacker communities, the word "fubar" had fallen out of use since the 1960s but has enjoyed another resurgence since it was used in the movies Tango and Cash (1989) and Saving Private Ryan (1998). FUBAR is also the title of a 2002 Canadian mockumentary.