0

I'm trying to log into a website using the auth cookies , using the techniques described in I'm reading through https://www.apharmony.com/software-sagacity/2014/10/using-wget-with-cookies/ .

I'm trying to turn my task into a bash script which I will use with git-bash. So far I have the tested wget with the cookies directly at the command line and the login works using:

wget --header "cookie: _ga=GA1.2.3865356.1523153047; ......" www.mysite.com

However in the Bash script , I have:

COOKIES="cookie:_ga=GA1.2.3865356.1523153047; ...."
wget --header $COOKIES www.mysite.com

This does not work. What am I doing wrong in the bash script?

user1592380
  • 34,265
  • 92
  • 284
  • 515
  • 1
    Does quoting help? I.e. `wget --header "$COOKIES" www.mysite.com` – dimid Apr 15 '18 at 19:17
  • @dimid that works, but why do you need to double quote? – user1592380 Apr 15 '18 at 19:52
  • Because it contains whitespace, as a general rule you should always quote variables. – dimid Apr 16 '18 at 04:59
  • what I meant was COOKIES="cookie:_ga=GA1.2.3865356.1523153047; ...." , but to expand it I have to use "$COOKIES" - 2 sets of double quotes – user1592380 Apr 16 '18 at 13:07
  • The first quotes are necessary since If you use `x=a b` in bash without quotes, it's interpreted as 1.assign `a` to the variable `x`, 2. run the command `b`. The second ones are necessary since `--header a b` will assign `a` to header and pass `b` as another argument. – dimid Apr 16 '18 at 16:11

1 Answers1

1

The value you assign to the COOKIES variable is without the quotes. You must either include the quotes within the variable, or quote the contents afterwards. The easiest solution seems to be:

COOKIES="'cookie:_ga=GA1.2.3865356.1523153047; ....'"
wget --header $COOKIES www.mysite.com
dr_agon
  • 323
  • 2
  • 8
  • Yes this works. Why do you need 2 sets of quotes ("'string'") instead of 1 set like in js or python? – user1592380 Apr 15 '18 at 19:28
  • The outside double quotes are processed when assigning the value to the variable, so in fact the variable contains only the string enclosed in single quotes when expanding, which is what you need. You can see the executed commands with expanded parameters by putting `set -x` at the beginning of your script. – dr_agon Apr 15 '18 at 20:02
  • The value of `$COOKIES`, unquoted, undergoes word-splitting, so the only part of the string used as the argument to `--header` is the part preceding the first space. Quoting prevents word-splitting. – chepner Apr 15 '18 at 21:50
  • @user61629 Can you double check that it works exactly as posted? It really shouldn't be. – that other guy Apr 16 '18 at 02:29
  • I ran it like wget --header "$COOKIES" www.mysite.com , and it worked – user1592380 Apr 16 '18 at 13:03
  • @user1592380 did you mean why bash uses double quotes (`"`) instead of single quotes (`'`)? You can have a string with single quotes is bash, however with it, what you write is what you get, without interpolation. If you write `echo '$COOKIES'` in single quotes, the shell would print the word $COOKIES, not the variable's value. See https://stackoverflow.com/questions/6697753/difference-between-single-and-double-quotes-in-bash for more info. – Tarek Jun 09 '23 at 19:48