I don't know why, but there's spaces and a newline after the URL when for example searching for aa
.
This probably won't work in all cases either, but here's a shell version: curl -s "http://duckduckgo.com/?q=%5C$(ruby -e 'require "cgi"; print CGI.escape(STDIN.read.chomp)' <<< example)" | sed -E "s/.*window\.location\.replace\('([^' ]+).*/\1/"
.