0

I have a folder containing file file1.png, file2.png, ..., file5.png. I'd like to copy them into a new folder and rename them as file6.png to file10.png. The bash script I used was:

for i in `seq 1 5`;
k=$((i+5));
do cp src/file$i.png dst/file$k.png;
done;

However, the line k=$((i+5)) always says syntax error. Any ideas?

3
  • 5
    do should come before k=$((i+5)) Commented Sep 1, 2016 at 7:47
  • 2
    shellcheck.net/# helps in such cases... Commented Sep 1, 2016 at 8:38
  • 1
    As written, the semi-colons are totally unnecessary. Commented Sep 1, 2016 at 12:11

3 Answers 3

4

Below script do the job :

for i in {1..5}
do # You need to put 'do' just after the for statement
cp src/file"$i".png dst/file"$((i+5))".png
done

Notes

  • See [ brace expansion ] to know more about {START..END}.

  • Note $((i+5)) returns the result to where it is called from, so you may avoid using the intermediate variable k.

Sign up to request clarification or add additional context in comments.

Comments

1

You forgot the do after the for:

for i in `seq 1 5`; do k=$((i+5)); echo $k; done;
#output:         ---^---
6
7
8
9
10

Comments

1

You have to do this-

for i in `seq 1 5`
do
k=$(($i+5))
cp src/file"$i".png dst/file"$k".png
done

3 Comments

No harm omitting $ inside ((..)).
As written, the semi-colons are totally unnecessary.
@user3439894, yup you're right. I was lazy during my copy pasting. I changed a one liner to a multi line comment for greater readability. Removed the semi colons now.

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.