Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Select an option

  • Save petergi/b44f8f96e937822a9d7dbf708141560a to your computer and use it in GitHub Desktop.

Select an option

Save petergi/b44f8f96e937822a9d7dbf708141560a to your computer and use it in GitHub Desktop.
Simple examples of reading a file in the shell one line at a time.
cat file.txt | while read line || [[ -n $line ]];
do
printf '%s\n' "$line" # do something with $line here
done
# Option 3: Single line at a time with a file descriptor
filename='file.txt'
exec 4<"$filename"
echo Start
while read -u4 p ; do
echo "$p"
done
# Option 2: Single line at a time with Input redirection
filename='file.txt'
echo Start
while read p; do
echo "$p"
done < "$filename"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment