0

I've been always using the following format for a "for loop" in a shell script :

example:

for i in {11001..110039}  
do
cp /home/usr/BB${i}  /home/usr/

now, I am getting the following error :

/home/usr/BB{11001..11039} does not exist

it should consider all the files BB11001 to BB11039, it was always working like this, and now I don't know why am I getting this error. Any help ?

3
  • is grammar right? should it be for i in words; do #statements done? Commented May 5, 2014 at 11:08
  • yes the syntax is like what you wrote Commented May 5, 2014 at 11:11
  • so does /home/usr/BB11001 exist? Commented May 5, 2014 at 11:29

2 Answers 2

2

You're probably using /bin/sh instead of /bin/bash.

Edit #1 (PoC):

$ bash --version
GNU bash, version 4.2.45(1)-release (x86_64-pc-linux-gnu)
Copyright (C) 2011 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>

This is free software; you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.

$ cat t.sh 
#!/bin/bash

for i in {1..5}
do
echo $i
done
$ ./t.sh
1
2
3
4
5
$

And with /bin/sh:

$ cat t.sh 
#!/bin/sh

for i in {1..5}
do
echo $i
done
$ ./t.sh
{1..5}
$
Sign up to request clarification or add additional context in comments.

4 Comments

Can you provide your bash version (if you're really using bash)?
GNU bash version 4.1.5
I'm not sure in which version that syntax was introduced, but v4.1.5 seems to be recent enough to implement it.
I've tried it now with version 4.2.45 and it's running flawlessly. Anyway, you can always use the for ((i=11001;i<=110039;i++)); syntax.
1

The syntax should be:

for i in {11001..110039}; do cp /home/usr/BB${i} /home/usr/; done

1 Comment

It's irrelevant. OP syntax is correct as far as he's using a 'done' statement after cp

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.