bash curl escape & in url


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting bash curl escape & in url
# 1  
Old 05-20-2010
bash curl escape & in url

I'm running a curl command in bash, but the & in the middle causes the second half of the line to run in the background, here's what I'm trying to do:
Code:
lat="37.451"
lon="-122.18"
url="http://ws.geonames.org/findNearestAddress?lat=$lat&lng=$lon"
curl -s "$url"

I tried escaping the & with \&, but it doesn't work. I couldn't seem to get the right combination of quotes to work either. If I curl another url from the same site, it works. If I type this url in a browser, it returns the proper xml
# 2  
Old 05-20-2010
wouldn't you want the url string to be wrapped in single-quotes...at the point of declaration...?
Code:
url='http://ws.geonames.org/findNearestAddress?lat=$lat&lng=$lon'
curl -s "$url"

or, better yet...
Code:
url="'http://ws.geonames.org/findNearestAddress?lat='${lat}'&lng='${lon}"
curl -s "$url"

or, actually:
Code:
url="$(echo 'http://ws.geonames.org/findNearestAddress?lat='${lat}'&lng='${lon} )"
curl -s "$url"


Last edited by curleb; 05-20-2010 at 04:24 PM.. Reason: foot-in-mouth disease...
# 3  
Old 05-20-2010
seems to break still...looking through curl docs and how to pass & to a variable...
# 4  
Old 05-20-2010
there was a typo in my last revision...$usrl is not the same as $url, believe it or not...

Sorry 'bout that...

Code:
-> lat="37.451"
lon="-122.18"

-> lon="-122.18"

-> url="'http://ws.geonames.org/findNearestAddress?lat='${lat}'&lng='${lon}"

-> echo $url                                                                
'http://ws.geonames.org/findNearestAddress?lat='37.451'&lng='-122.18

-> url='http://ws.geonames.org/findNearestAddress?lat='${lat}'&lng='${lon}  

-> echo $url                                                              
http://ws.geonames.org/findNearestAddress?lat=37.451&lng=-122.18

-> echo 'http://ws.geonames.org/findNearestAddress?lat='${lat}'&lng='${lon}
http://ws.geonames.org/findNearestAddress?lat=37.451&lng=-122.18

This User Gave Thanks to curleb For This Post:
# 5  
Old 05-20-2010
both of these work
Code:
url='http://ws.geonames.org/findNearestAddress?lat='${lat}'&lng='${lon}''

or
Code:
url="$(echo 'http://ws.geonames.org/findNearestAddress?lat='${lat}'&lng='${lon} )"

thanks!

Last edited by unclecameron; 05-20-2010 at 04:58 PM..
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Escape bash-special character in a bash string

Hi, I am new in bash scripting. In my work, I provide support to several users and when I connect to their computers I use the same admin and password, so I am trying to create a script that will only ask me for the IP address and then connect to the computer without having me to type the user... (5 Replies)
Discussion started by: arcoa05
5 Replies

2. Shell Programming and Scripting

Using CURL command with special characters in URL

Hi. I 'm trying to hit a REST api and retrieve a JSON feed, and the URL has special characters in it. Something like: Example Domain The below curl command is failing curl -X GET https://www.example.com/?sample=name&id=1001 saying bad command at id=1001 I am going to use this as part... (3 Replies)
Discussion started by: kumarjt
3 Replies

3. Shell Programming and Scripting

Use curl to send a static xml file using url encoding to a web page using pos

Hi I am try to use curl to send a static xml file using url encoding to a web page using post. This has to go through a particular port on our firewall as well. This is my first exposure to curl and am not having much success, so any help you can supply, or point me in the right direction would be... (1 Reply)
Discussion started by: Paul Walker
1 Replies

4. Shell Programming and Scripting

Read URL data from UNIX-CLI without Wget,CURL,w3m,LWP

Hi Experts, Problem statement : We have an URL for which we need to read the data and get parsed inside the shell scripts.My Aix has very limited perl utility, i cant install any utility as well. Precisely, wget,cURL,Lynx,w3m and Lwp cant be used as i got these utilities only when i googled it.... (12 Replies)
Discussion started by: scott_cog
12 Replies

5. UNIX for Dummies Questions & Answers

Read URL data from UNIX without wget,curl,lynx,w3m.

Hi Experts, Problem statement : We have an URL for which we need to read the data and get parsed inside the shell scripts. My Aix has very limited perl utility, i cant install any utility as well. Precisely, wget,cURL,Lynx,w3m and Lwp cant be used as i got these utilities only when i googled... (0 Replies)
Discussion started by: scott_cog
0 Replies

6. UNIX for Dummies Questions & Answers

Launch a URL,validate username and password using wget or curl

Hi All, I want to launch "ex: http://gmail.com" from the cmd window and validate the credentials with username and password, is it possible? I have found something like this "wget --http-user=USER' --http-password=PASSWORD http://gmail.com" am new to this and unable to find a solution, i... (0 Replies)
Discussion started by: harsha85
0 Replies

7. Shell Programming and Scripting

Downloading of dynamically generated URL using curl and sed

I've been attempting to use curl and sed to allow for downloading a file from a dynamically generated URL. I've been able to retrieve and save the HTML of the page that does the dynamic generation of the download URL using curl but I'm very new to sed and I seem to be stuck at this part. HTML: ... (1 Reply)
Discussion started by: schwein
1 Replies

8. Shell Programming and Scripting

How to achieve UTF-8 encoding & URL escape in an xml file?

Is there any i can achieve entity escaping, URL escaping & UTF-8 encoded for the xml generated through shell script? #! /bin/bash echo "<path>" >> file.xml for x in `ls filename*` do echo -e "\t<dir>" >> file.xml echo -e "\t\t<file>$x</file>" >> file.xml... (0 Replies)
Discussion started by: vel4ever
0 Replies

9. Shell Programming and Scripting

Replace new line with <br /> & escape special characters

Hi, I wish to replace a new line with br (html) but it doesn't seem to work message=$(echo ${FORM_message} | tr '\r' '<br \/>' ) what it gives me seems to be ... b...? I am also having problem escaping hash sign in cut command: list=$(echo "$line" | cut -d'\#;\#' -f1) ; my intention is... (2 Replies)
Discussion started by: ted_chou12
2 Replies

10. Shell Programming and Scripting

Splitting URL request string including escape characters

Hi All, I have a text file containing two fields, the first field holds the website name and the second one holds the rest of the URL (Request part). My requirement is to split the request part based on the characters "=" and "&". The process should also consider the URL escape characters, for... (4 Replies)
Discussion started by: john2022
4 Replies
Login or Register to Ask a Question