-
Notifications
You must be signed in to change notification settings - Fork 1.9k
SC2013
kkmuffme edited this page Jan 13, 2025
·
12 revisions
for line in $(cat file | grep -v '^ *#')
do
echo "Line: $line"
donegrep -v '^ *#' file | while IFS= read -r line
do
echo "Line: $line"
doneor without a subshell (bash, zsh, ksh):
while IFS= read -r line
do
echo "Line: $line"
done < <(grep -v '^ *#' file)or without a subshell, with a pipe (more portable, but write a file on the filesystem):
mkfifo mypipe
grep -v '^ *#' file > mypipe &
while IFS= read -r line
do
echo "Line: $line"
done < mypipe
rm mypipeNOTE: grep -v '^ *#' is a placeholder example and not needed. To just loop through a file:
while IFS= read -r line
do
echo "Line: $line"
done < file
# or: done <<< "$variable"For loops by default (subject to $IFS) read word by word. Additionally, glob expansion will occur.
Given this text file:
foo *
barThe for loop will print:
Line: foo
Line: aardwark.jpg
Line: bullfrog.jpg
...The while loop will print:
Line: foo *
Line: barIf you do want to read word by word, you can set $IFS appropriately and disable globbing with set -f, and then ignore this warning. Alternatively, you can pipe through tr ' ' '\n' to turn words into lines, and then use while read. In Bash/Ksh, you can also use a while read -a loop to get an array of words per line.