Reading a File in Bash Shell Script

By | February 28, 2011

At times you might want to read a file or give your shell a file to take input from it. I had the same situation while I was practicing at home. So, here is the code that worked out for me. Hope this can help you too. I might edit this page latest if I find better way of doing the same. So, here is the code for you.

#!/bin/bash
infile=`cat /home/vamsi/input`
for i in $infile; do
echo $i
done

Here is yet another script that gives you the exact out put. This script reads the line using while loop in shell.

#!/bin/bash

while read line
do
echo $line
done

infile is a file that is in the same directory where the script exists.

If you like this article, then please consider sharing it. Sharing really inspires us to do things much better!!

Leave a Reply

Your email address will not be published. Required fields are marked *