====== BASH - Files - Read a file - Troubleshooting - Prevent other commands from "eating" the input ======
Some commands greedily eat up all available data on standard input.
For example:
while read -r line; do
cat > ignoredfile
printf '%s\n' "$line"
done < "$file"
**NOTE:** This will only print the contents of the first line.
The remaining content will go to "ignoredfile", as cat slurps up all available input.
----
One workaround is to use a numeric FileDescriptor rather than standard input:
# Bash
while IFS= read -r -u 9 line; do
cat > ignoredfile
printf '%s\n' "$line"
done 9< "$file"
**NOTE:** **read -u** is not portable to every shell.
Use a redirect to ensure it works in any POSIX compliant shell:
while IFS= read -r line <&9; do
cat > ignoredfile
printf '%s\n' "$line"
done 9< "$file"
or:
exec 9< "$file"
while IFS= read -r line <&9; do
cat > ignoredfile
printf '%s\n' "$line"
done
exec 9<&-
This example will wait for the user to type something into the file ignoredfile at each iteration instead of eating up the loop input.
Other commands that act this way include ssh and ffmpeg.