Looping through the content of a file in Bash

后端 未结 13 2403
傲寒
傲寒 2020-11-21 10:08

How do I iterate through each line of a text file with Bash?

With this script:

echo \"Start!\"
for p in (peptides.txt)
do
    echo \"${p}\"
done
         


        
13条回答
  •  梦毁少年i
    2020-11-21 10:33

    Option 1a: While loop: Single line at a time: Input redirection

    #!/bin/bash
    filename='peptides.txt'
    echo Start
    while read p; do 
        echo $p
    done < $filename
    

    Option 1b: While loop: Single line at a time:
    Open the file, read from a file descriptor (in this case file descriptor #4).

    #!/bin/bash
    filename='peptides.txt'
    exec 4<$filename
    echo Start
    while read -u4 p ; do
        echo $p
    done
    

提交回复
热议问题