-1

Hard to google this one, I have this:

echo "age" |  while read line; do
    echo "$line"
done

but there there is this style:

while read line; do
    echo "$line"
done < echo "age"

first, the second style is not quite right, but is there a name for the first and second styles? is there any functional/behavioral difference?

1 Answers1

1

The biggest functional difference is that the first (in bash) will run the loop in a subshell. As a result, $line will lose its value after the loop is done. To avoid the subshell, you can embed the content directly in the shell with a heredoc:

while read line; do
    echo "$line"
done << EOF
age
EOF
William Pursell
  • 204,365
  • 48
  • 270
  • 300