Benoît
Benoît

Reputation: 85

Variable, incrementation and its output

Here is a script:

VarTest01=1
VarTest02=$( printf '%s-%03d' text $VarTest01 )

while [ $VarTest01 -lt 5 ]; do
echo $VarTest02
(( VarTest01++ ))
done

here is its output:

text-001
text-001
text-001
text-001

My question is: why don't I get "text-001" to "text-004"? It seems $VarTest01 is being incremented but not displayed properly. What am I missing?

Upvotes: 0

Views: 31

Answers (1)

VarTest01=1
VarTest02=$( printf '%s-%03d' text $VarTest01 )

while [ $VarTest01 -lt 5 ]; do
echo $VarTest02
(( VarTest01++ ))
done

You are incrementing VarTest01 but you only declare the string of VarTest02 once, so I think this would work:

VarTest01=1


while [ $VarTest01 -lt 5 ]; do
VarTest02=$( printf '%s-%03d' text $VarTest01 )
echo $VarTest02
(( VarTest01++ ))
done

Upvotes: 4

Related Questions