Hello.
I am very newbie in bash coding, I have a file with the elements:
apple
pear
juice
orange
I need to read this file in bash script, and print those values and the number of elements line by line attached to those values, for example what I need to print as output is:
1 apple
2 pear
3 juice
4 orange
I know I can read the elements from a file with the command "wc -l" but i don,t want the total of elements printed on screen, I want line per line printed. In Perl I know I can do this with foreach:
Code:
foreach my $value (0..$#fruits) {
print $value + 1 . " $fruits[$value]\n";
}
will probably print on screen:
1 apple
2 pear
3 juice
4 orange
But since I don,t have foreach in bash, is kind of hard for me to translate from perl to bash. What I have done in bash for reading the file is:
Code:
file=($(cat myfruits.txt))
for a in ${file[*]}
do
echo $a
done
Any help will be very appreciated.