To complement @Trauma's response.
"for" does a little more than iterate over a set of values, or rather, that set of values is generated to then iterate over it, also operating under premises, as would be done in c and it does in several Ways:
for name in < secuencia separada por espacios >;do lista de comandos;done
for ((expr1;expr2;expr3));do lista de comandos;done
This can be found in man 1 bash
.
I mention this because after in, in example 1, the for loop works on expansions, that is:
for archivo in carpeta/*
do
echo $archivo
done
#es equivalente a
for archivo in "$(ls carpeta)"
do
echo $archivo
done
They are equivalent but they work with different types of expansions, the first was on pathname expansion and the second was on command substitution, but it can also be done on brace expansion, that is:
for i in {1..4}
do
echo $i
done
1
2
3
4
Or a slightly more complex example in which you can combine them:
for archivo in ca1/sc{2,3}/*foo
do
echo $archivo
done
It will print all the files that end with foo and that are inside the sc2 and sc3 folders that are also inside the ca1 folder.
You can also see the expansions in man bash
, in the EXPANSIONS section. It is important to note that the order in which the chains are expanded has an order of priority.
I say this about the expansions because I think it is necessary to clarify it since there are questions like these:
In the second example that @Alexlex put, you can see that you can also iterate on the expansion of brace expansion on an array.