Skip to content

Instantly share code, notes, and snippets.

@bluegraybox
Created February 15, 2012 03:01
Show Gist options
  • Save bluegraybox/1832793 to your computer and use it in GitHub Desktop.
Save bluegraybox/1832793 to your computer and use it in GitHub Desktop.
Bash 'for' loop over lines, not words
#!/bin/bash
# Only split on newlines, so each line of data is one record.
default_IFS=$IFS
IFS="$(echo -e "\n\r")"
# each $line will be one line, even if there are spaces in it.
for line in $(cat sample_data.txt) ; do
# do stuff...
done
# Restore default
IFS=$default_IFS
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment