I would like to generate a local array in a shell/bash script that consists of the following elements: JF-1998, JF-1999, JF-2000,... , JF-2011. That is, a string JF- combined with the years from 1998 to 2000.

I would also like to be able to do the reverse: Combine a number with a limited number of strings before that number: JF-1998, RFS-1998, JFI-1998.

Are there simple ways to do so?

  • 5
    You mean something like arr=( JF-{1998..2011} )? – steeldriver Dec 28 '14 at 0:14
  • @steeldriver: I added a new part to the question because I didn't want to cast a new one. Do you also know how to deal with combining some strings with a fixed number? – MERose Dec 28 '14 at 22:33
  • @MERose The converse of that usage: {JF,RFS,JFI}-1998 – muru Dec 28 '14 at 22:59
  • @steeldriver Can I put JF,RFS,JFI in a variable? For me it didn't work when in do names=(JF,RFS,JFI) and then {$names}-1998. Also no spaces instead of commas. – MERose Dec 28 '14 at 23:55
  • This type of variable expansion is known as brace expansion because you need braces: for example echo {JF,RFS,JFI}-1998 – steeldriver Dec 29 '14 at 0:02

As steeldriver answered and muru answered, bash has a brace expansion feature that can solve your problem:

array1=( JF-{1998..2011} )


array2=( {JF,RFS,JFI}-1998} )

or even all together:

array3=( JF-{1998..2011} {JF,RFS,JFI}-1998} )
| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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