I'm writing a bash script to automatize my downloading process. In a section, it gaves a error:

syntax error near unexpected token `newURL=$(echo $URL | sed 's/&:/$index/')'

That section of code is here:

# Downloading the pdf's.
for index in {$startedNumber..$endedNumber} do
    newURL=$(echo $URL | sed 's/&:/$index/')
    wget $newURL

This section handles a URL like "http://xxxx.com/xxx/yyy/zzz&:.pdf" and find "&:" part. When it finds that, it will change it to $index. So that I can download these lines: http://xxxx.com/xxx/yyy/zzz1.pdf http://xxxx.com/xxx/yyy/zzz2.pdf http://xxxx.com/xxx/yyy/zzz3.pdf http://xxxx.com/xxx/yyy/zzz4.pdf (... and goes on until de $endedNumber)

What should I do to solve this problem?


There are a few issues here in addition to those addressed in current answers

  1. bash doesn't (directly) support the use of shell variables inside a brace expansion. See for example

  2. $index wont get expanded because your sed expression is single-quoted. See for example

  3. avoid using ALL-CAPS variables names and don't forget to double-quote variable expansions

In fact you don't need sed at all since the shell itself can do simple search and replace on variables directly. So for example

startedNumber=3; endedNumber=7


for index in $(seq $startedNumber $endedNumber); do 
  echo wget "${url/&:/$index}"
wget http://foo3bar
wget http://foo4bar
wget http://foo5bar
wget http://foo6bar
wget http://foo7bar

(remove the echo for your real case).

| improve this answer | |

The do keyword should either be on a new line or after a ;.

General rule of thumb from personal experience, when an error is reported as being near an unexpected token, always look at the code before it (not just in bash)

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.