How do i use the read command to read from a file?



  • As well as well as taking interactive input, the read command can read from a file too.

    Take a look at the following script; the inputfile.txt is in the same directory as the script.

    $ cat readthis.sh 
    #!/bin/bash
    while read line
    do
       echo $line | sed -e 's/line/item/'
    done < inputfile.txt
    

    We read line by line and use the sed command to change a word. The change is not permanent. We just used sed to demonstrate a modification of the lines. Between the do and done you process $line however you want.


Log in to reply
 

© Lightnetics 2024