1

I have a problem that I just cannot seem to solve.

I need to make a curl request to a given URL, and the URL requires a dollar sign in it.

So, for example:

www.example.com/mypath/function&$filter=whatever

Now, I can execute this just fine from the command line if I put the URl in single ticks, or if I escape the dollar sign with a backslash, and then put it in a double quote.

Obviously, there is a problem if you do not do either of the above, because bash will see the '$' and will interpret anything after it to mean a variable name.

So when I try:

URL="www.example.com/mypath/function&\\\$filter=whatever"
MYOUTPUT=$(curl -s --header "Authorization: $HEADER" "$URL")

it doesn't work right.

When I try

URL="www.example.com/mypath/function&\$filter=whatever"
MYOUTPUT=$(curl -s --header "Authorization: $HEADER" "'$URL'")

it doesn't work right.

What am I doing wrong?

I can tell it's not working right because the server is not responding the same way in the script as it does in the command line. The site responds in a certain default manner if the query isn't done right, and I always get the default response through the script.

jasonmclose
  • 1,667
  • 4
  • 22
  • 38
  • Can you give me the question I am duplicating? Yeah, folks have asked questions about curl and scripting, but I haven't seen anything about how to deal with a rogue dollar sign. – jasonmclose Jul 14 '17 at 03:24

1 Answers1

2

If you put a literal $ in a string, bash will not try to interpret it in future expansions:

URL='www.example.com/mypath/function&$filter=whatever'
MYOUTPUT=$(curl -s --header "Authorization: $HEADER" "$URL")
that other guy
  • 116,971
  • 11
  • 170
  • 194