Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

I want to create 5 files with the same name but with an incremental counter.

If I do:

for i in {1..5}; do touch hallo_$i; done

I get hallo_1, hallo_2, hallo_3, etc.

If I do:

for i in {1..5}; do touch company-price-$i_spec.rb; done

I get: company-price-.rb

Do I have to escape something? Could it be that bash thinks I want to remove/subtract from the $i variable?

If I escape . and - I still got the same result. Any hints?

share|improve this question
add comment

1 Answer 1

up vote 4 down vote accepted

You must use the braced form of variable expansion:

for i in {1..5}; do touch company-price-${i}_spec.rb; done

otherwise, bash will see $i_spec as a variable expansion, not $i.

From bash manual:

The basic form of parameter expansion is ${parameter}. The value of 
parameter is substituted. The braces are required when parameter is a
positional parameter with more than one digit, or when parameter is 
followed by a character that is not to be interpreted as part of its name.
share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.