Bird
Raised Fist0
Bash Scriptingscripting~10 mins

Reading files line by line (while read) in Bash Scripting - Interactive Code Practice

Choose your learning style10 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to read a file line by line using a while loop.

Bash Scripting
while [1] read line; do
  echo "$line"
done < file.txt
Drag options to blanks, or click blank then click option'
Ado
Bcat
CIFS=
Dfor
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'for' loop instead of 'while' to read lines
Not setting IFS, causing trimming of spaces
Using 'cat' incorrectly inside the while loop
2fill in blank
medium

Complete the code to read a file named 'data.txt' line by line and print each line.

Bash Scripting
while IFS= read [1]; do
  echo "$line"
done < data.txt
Drag options to blanks, or click blank then click option'
Atext
Bline
Cfile
Dword
Attempts:
3 left
💡 Hint
Common Mistakes
Using variable names that do not match the echo statement
Using reserved words as variable names
3fill in blank
hard

Fix the error in the code to correctly read lines from 'input.txt'.

Bash Scripting
while IFS= read line; do
  echo "$line"
done < [1]
Drag options to blanks, or click blank then click option'
Ainput.txt
Bfile.txt
Coutput.txt
Ddata.txt
Attempts:
3 left
💡 Hint
Common Mistakes
Using the wrong filename in the input redirection
Missing the '<' symbol before the filename
4fill in blank
hard

Fill both blanks to read 'myfile.txt' line by line and count lines longer than 5 characters.

Bash Scripting
count=0
while [1] read line; do
  if [ ${#line} [2] 5 ]; then
    count=$((count + 1))
  fi
done < myfile.txt
echo $count
Drag options to blanks, or click blank then click option'
AIFS=
B>
C-gt
D==
Attempts:
3 left
💡 Hint
Common Mistakes
Using '>' instead of '-gt' in test brackets
Not setting IFS= causing trimmed lines
5fill in blank
hard

Fill all three blanks to create a dictionary of word lengths for words longer than 3 characters.

Bash Scripting
declare -A lengths
for word in apple banana cat dog elephant; do
  if [ ${#word} [1] 3 ]; then
    lengths[[2]]=[3]
  fi
done
for k in "${!lengths[@]}"; do
  echo "$k: ${lengths[$k]}"
done
Drag options to blanks, or click blank then click option'
A-gt
Bword
C${#word}
D==
Attempts:
3 left
💡 Hint
Common Mistakes
Using '==' instead of '-gt' for numeric comparison
Using wrong keys or values in the associative array

Practice

(1/5)
1. What does the command while IFS= read -r line; do echo "$line"; done < file.txt do in a bash script?
easy
A. Deletes the contents of file.txt.
B. Reads the entire file at once and prints it.
C. Writes each line of file.txt to a new file.
D. Reads the file file.txt line by line and prints each line.

Solution

  1. Step 1: Understand the while read loop

    The loop reads input line by line from the redirected file file.txt.
  2. Step 2: Check the action inside the loop

    Each line read is stored in variable line and printed with echo.
  3. Final Answer:

    Reads the file file.txt line by line and prints each line. -> Option D
  4. Quick Check:

    Line-by-line reading = A [OK]
Hint: Remember: while read loops read files line by line [OK]
Common Mistakes:
  • Thinking it reads the whole file at once
  • Confusing input redirection with output
  • Ignoring the role of IFS= and -r
2. Which of the following is the correct syntax to read a file line by line in bash?
easy
A. while read line; do echo $line; done < file.txt
B. while read -r line; do echo $line; done > file.txt
C. while read line; do echo $line; done >> file.txt
D. while read line; do echo $line; done | file.txt

Solution

  1. Step 1: Identify input redirection for reading

    The correct way to read from a file is using input redirection: < file.txt.
  2. Step 2: Check the loop syntax

    while read line; do ... done is the standard loop to read lines.
  3. Final Answer:

    while read line; do echo $line; done < file.txt -> Option A
  4. Quick Check:

    Input redirection with < = C [OK]
Hint: Use < file.txt to feed file lines into the loop [OK]
Common Mistakes:
  • Using output redirection (>) instead of input (<)
  • Piping file name instead of redirecting input
  • Missing the -r option to prevent backslash escapes
3. What will be the output of this script if file.txt contains:
apple
banana
cherry

Script:
while IFS= read -r line; do echo "Fruit: $line"; done < file.txt
medium
A. Fruit: apple Fruit: banana Fruit: cherry
B. apple banana cherry
C. Fruit: apple banana cherry
D. Syntax error

Solution

  1. Step 1: Understand the loop reading each line

    The loop reads each line from file.txt separately.
  2. Step 2: Check the echo command

    Each line is printed prefixed with "Fruit: ".
  3. Final Answer:

    Fruit: apple Fruit: banana Fruit: cherry -> Option A
  4. Quick Check:

    Line prefixing with echo = B [OK]
Hint: Each loop iteration prints one line with prefix [OK]
Common Mistakes:
  • Expecting all lines printed on one line
  • Ignoring the prefix in echo
  • Confusing input and output redirection
4. Identify the error in this script snippet:
while read line; do echo $line; done file.txt
medium
A. Echo command syntax is wrong
B. Incorrect variable name used
C. Missing input redirection operator < before file.txt
D. Missing semicolon after read

Solution

  1. Step 1: Check how the file is provided to the loop

    The file must be redirected as input using < file.txt, not as an argument.
  2. Step 2: Confirm syntax correctness

    The rest of the syntax is correct; variable and echo usage are fine.
  3. Final Answer:

    Missing input redirection operator < before file.txt -> Option C
  4. Quick Check:

    Input redirection needed = D [OK]
Hint: Use < file.txt to feed file lines, not argument [OK]
Common Mistakes:
  • Passing filename as argument instead of redirecting input
  • Forgetting semicolon after read (optional here)
  • Misusing echo syntax
5. You want to read a file line by line and skip empty lines. Which script snippet correctly does this?
hard
A. while read line; do echo "$line"; done < file.txt | grep '^$'
B. while IFS= read -r line; do if [[ -n $line ]]; then echo "$line"; fi; done < file.txt
C. cat file.txt | while read line; do echo "$line"; done
D. while IFS= read -r line; do echo "$line"; done < file.txt

Solution

  1. Step 1: Understand skipping empty lines

    We need to check if the line is not empty using [[ -n $line ]].
  2. Step 2: Confirm reading and condition

    while IFS= read -r line; do if [[ -n $line ]]; then echo "$line"; fi; done < file.txt reads lines safely and prints only non-empty lines inside the loop.
  3. Final Answer:

    while IFS= read -r line; do if [[ -n $line ]]; then echo "$line"; fi; done < file.txt -> Option B
  4. Quick Check:

    Condition inside loop filters empty lines = A [OK]
Hint: Use if [[ -n $line ]] inside loop to skip empty lines [OK]
Common Mistakes:
  • Using external grep after loop (may lose variable scope)
  • Not checking for empty lines inside the loop
  • Using unnecessary cat command
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard