Dave's Brain

Browse - Programming Tips - How can I increment a variable in a loop?

Date: 2016mar6
Language: bash

Q.  How can I increment a variable in a loop?

A.  The modern way to increment a variable in bash is:

((var++))
where var is the name of your variable. In a loop:
i=1 for orig in `ls *.jpg`; do padded=`printf '%03d' $i` mv $orig $padded.jpg ((i++)) done

Add a comment

Sign in to add a comment
Copyright © 2008-2017, dave - Code samples on Dave's Brain is licensed under the Creative Commons Attribution 2.5 License. However other material, including English text has all rights reserved.