Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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
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

Your Answer


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.