Setting wget -header = in bash does not work

I am trying to set the title in wget. When I run the following command in the terminal, it works wget -d --header="User-Agent: Mozilla/5.0 (Windows NT 6.0) AppleWebKit/537.11 (KHTML, like Gecko) Chrome/23.0.1271.97 Safari/537.11"' http://website.com -O index

but as soon as I put the same thing in variables and try to run a bash script, it does not work.

what i tried

 header='-d --header="User-Agent: Mozilla/5.0 (Windows NT 6.0) AppleWebKit/537.11 (KHTML, like Gecko) Chrome/23.0.1271.97 Safari/537.11"' wget "$header" http://google.com -O index 

Error

 wget: invalid option -- ' ' wget: invalid option -- '-' wget: invalid option -- '-' Usage: wget [OPTION]... [URL]... 
+4
source share
3 answers

When using a variable, you need to use double quotes. Otherwise, it will be expanded to a few words. On the other hand, it is not necessary to quote the value of a variable twice. The following should work:

 header='--header=User-Agent: Mozilla/5.0 (Windows NT 6.0) AppleWebKit/537.11 (KHTML, like Gecko) Chrome/23.0.1271.97 Safari/537.11' wget "$header" http://website.com -O index 

Edit: If you want to use variables for multiple arguments, use arrays:

 args=(-d '--header=User-Agent: Mozilla/5.0 (Windows NT 6.0) AppleWebKit/537.11 (KHTML, like Gecko) Chrome/23.0.1271.97 Safari/537.11') wget "${args[@]}" http://website.com -O index 
+9
source

COMMENT: β€œI didn’t like the nosid trick about the bash array. In my case, the corresponding code:

 WGET_OPTS="-r -N -nd -np -nH --timeout=120 --tries=3" WGET_OPTS_ARRAY=(${WGET_OPTS// / }) wget "${WGET_OPTS_ARRAY[@]}" -A "$FILE_PAT" -P "$TO_DIR" "$FROM_URL" 
+1
source

Instead of this

 wget $header http://website.com -O index 

try it,

 wget "$header" http://website.com -O index 

The spaces in the header text are broken when you assign it a header variable. To get rid of the problem, you must surround the variable with quotation marks "".

After comments: Try this -

 someheader="-d --header='User-Agent: Mozilla/5.0 (Windows NT 6.0) AppleWebKit/537.11 (KHTML, like Gecko) Chrome/23.0.1271.97 Safari/537.11'" wget "$someheader" http://website.com -O index 

The title of the title seems to contradict your --header. Or maybe quotation marks, not copy and repeat them. However strange!

0
source

Source: https://habr.com/ru/post/1495362/


All Articles