[SOLVED] bash changes { to %7B and } to %7D

About writing shell scripts and making the most of your shell
Forum rules
Topics in this forum are automatically closed 6 months after creation.
Locked
tolga

[SOLVED] bash changes { to %7B and } to %7D

Post by tolga »

Hi Everyone,

I am running into a weird issue for the past days.

I have a simple script that downloads files with wget as follows:

Code: Select all

wget -r -np -nd http://website.com/document{01..20}.pdf
wget -r -np -nd http://somesite.com/document{01..20}.pdf
wget -r -np -nd http://anothersite.com/document{01..20}.pdf
However when I run the script, the terminal output shows (below is one example. The error is the same for each address - website.com, somesite.com, anothersite.com).

Code: Select all

--2018-08-29 18:54:41--  http://website.com/document%7B01..20%7D.pdf
Resolving website.com
Connecting to website.com... connected.
HTTP request sent, awaiting response... 404 Not Found
2018-08-29 18:54:41 ERROR 404: Not Found.
I thought that the script file could be corrupt and I wrote it from scratch in a new file. That did not change anything.

When I copied the lines from the file and ran from the terminal, it executed without any errors:

Code: Select all

wget -r -np -nd http://website.com/document{01..20}.pdf ; wget -r -np -nd http://somesite.com/document{01..20}.pdf ;wget -r -np -nd http://anothersite.com/document{01..20}.pdf
Any ideas on what can be wrong?

Thank you.
Tolga
Last edited by LockBot on Wed Dec 28, 2022 7:16 am, edited 2 times in total.
Reason: Topic automatically closed 6 months after creation. New replies are no longer allowed.
FreedomTruth
Level 4
Level 4
Posts: 443
Joined: Fri Sep 23, 2016 10:19 am

Re: bash changes { to %7B and } to %7D

Post by FreedomTruth »

Are you positive your script is using bash (not "sh")? #!/bin/bash
It seems to all work correctly on my system, wget directly from terminal or in a script file.

(edit) just tried with dash instead, it came up with the same thing you were experiencing, trying to get a single file named "document{01..20}.pdf"
tolga

Re: bash changes { to %7B and } to %7D

Post by tolga »

Hi @FreedomTruth,

Thank you very much for your reply.

I am positive that I use /bin/bash. And, following your point, I tried with dash and I got the same result.

It seems very weird that the exact same line works when run through the terminal but not from the script.

Frankly I have no idea what is going on and where to start troubleshooting :(
FreedomTruth
Level 4
Level 4
Posts: 443
Joined: Fri Sep 23, 2016 10:19 am

Re: bash changes { to %7B and } to %7D

Post by FreedomTruth »

Hmm.. how are you calling the script?
./script.sh is not the same as sh ./script.sh

Other than that, I'm also confused why it would behave differently.

Code: Select all

$ cat test.sh
#!/bin/bash
echo {01..10}
$ ./test.sh
01 02 03 04 05 06 07 08 09 10
$ dash ./test.sh
{01..10}
$
gm10

Re: bash changes { to %7B and } to %7D

Post by gm10 »

tolga wrote: Wed Aug 29, 2018 2:41 pm I am positive that I use /bin/bash.
Almost certainly not. Run the script like this bash <script> and you'll most likely see it works. Also post the full script contents please.
User avatar
smurphos
Level 18
Level 18
Posts: 8501
Joined: Fri Sep 05, 2014 12:18 am
Location: Irish Brit in Portugal
Contact:

Re: bash changes { to %7B and } to %7D

Post by smurphos »

Shebang your script. As has been posted this works fine in bash. Neighter sh or dash support brace expansion.

Code: Select all

#!/bin/bash
wget -r -np -nd http://website.com/document{01..20}.pdf
wget -r -np -nd http://somesite.com/document{01..20}.pdf
wget -r -np -nd http://anothersite.com/document{01..20}.pdf
For custom Nemo actions, useful scripts for the Cinnamon desktop, and Cinnamox themes visit my Github pages.
tolga

Re: bash changes { to %7B and } to %7D

Post by tolga »

Thank you very much @gm10 and @smurphos. This was really driving me nuts!

I used both of your suggestions and it worked like a charm:
  1. I shebanged my script,
  2. I ran it with bash my_script.sh
Thank you very much for your kind assistance once again!
Locked

Return to “Scripts & Bash”