From this site:
for i in $(seq 1 10);
do
echo $i
done
Answer from Rob Rolnick on Stack OverflowHow do I loop over command-line arguments?
How do I loop over lines in a file?
What is the difference between the standard `for` loop and the C-style `for` loop?
Videos
I'm extremely comfortable with the standard format of a for loop
for(x=0; x<10; x++){
actions
}But apparently this syntax is not allowed in Bash? Why!?!??
What's the closest possible format I can use in Bash that resembles the format above? I thought that was the standard format for all programming languages.
I've searched everywhere online and can't find anything about this.
One way to do it is:
while read p; do
echo "$p"
done <peptides.txt
As pointed out in the comments, this has the side effects of trimming leading whitespace, interpreting backslash sequences, and skipping the last line if it's missing a terminating linefeed. If these are concerns, you can do:
while IFS="" read -r p || [ -n "$p" ]
do
printf '%s\n' "$p"
done < peptides.txt
Exceptionally, if the loop body may read from standard input, you can open the file using a different file descriptor:
while read -u 10 p; do
...
done 10<peptides.txt
Here, 10 is just an arbitrary number (different from 0, 1, 2).
cat peptides.txt | while read line
do
# do something with $line here
done
and the one-liner variant:
cat peptides.txt | while read line; do something_with_$line_here; done
These options will skip the last line of the file if there is no trailing line feed.
You can avoid this by the following:
cat peptides.txt | while read line || [[ -n $line ]];
do
# do something with $line here
done