I have several input files to run a program, and I organized them in a way that the names of the input files are numbers. I would like to know if it is possible to write a loop to call these different input files, or in others words, to call a input file which name is a variable of the loop.

To be more specific, I created the input files in fortran, which names are "fort.XXX", where the XXX is a number, from 1 to 100. I call each of this inputs with a command like "program fort.XXX" Is there a way to create a loop in which I will write a command that is part constant ("program fort.") and part variable ("XXX")?

Many thanks in advance,


2 Answers 2


If you want to use the names of those existing files you can use something like this:

for FILE in fort.*
    echo "$FILE"

It will use all files in the current directory starting from fort. regardless of number of files. Of course, you can use any other command in place of echo, here.


Here's an example that uses a range with the bash for loop:

for i in {1..100}
echo "program fort.${i}"

Which should give you the following output:

program fort.1
program fort.2
program fort.3
program fort.4
program fort.99
program fort.100

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .