0
0
Bash Scriptingscripting~10 mins

for loop (list-based) in Bash Scripting - Interactive Code Practice

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

Complete the code to loop over the list of fruits and print each one.

Bash Scripting
for fruit in [1]; do
  echo "$fruit"
done
Drag options to blanks, or click blank then click option'
A(apple banana cherry)
B"apple banana cherry"
Capple banana cherry
D[apple banana cherry]
Attempts:
3 left
💡 Hint
Common Mistakes
Using quotes around the list makes it a single string, not a list.
Using parentheses or brackets is not valid for list iteration in bash.
2fill in blank
medium

Complete the code to loop over the list and print each item in uppercase.

Bash Scripting
for item in apple banana cherry; do
  [1] "$item"
done
Drag options to blanks, or click blank then click option'
Aprintf
Becho
Ccut
Dtr '[:lower:]' '[:upper:]' <<<
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'echo' alone does not change case.
Using 'cut' does not change letter case.
3fill in blank
hard

Fix the error in the loop to correctly iterate over the list and print each element.

Bash Scripting
for i in [1]; do
  echo "$i"
done
Drag options to blanks, or click blank then click option'
A"apple banana cherry"
Bapple banana cherry
Capple,banana,cherry
D(apple banana cherry)
Attempts:
3 left
💡 Hint
Common Mistakes
Using commas breaks the list into one string.
Using quotes makes the entire list one item.
4fill in blank
hard

Fill both blanks to create a loop that prints only fruits with names longer than 5 letters.

Bash Scripting
for fruit in apple banana cherry date; do
  if [[ ${#fruit} [1] 5 ]]; then
    echo $[2]
  fi
done
Drag options to blanks, or click blank then click option'
A>
B"$fruit"
C<
Decho
Attempts:
3 left
💡 Hint
Common Mistakes
Using '<' would print fruits with names shorter than 5 letters.
Printing 'echo' instead of the variable name.
5fill in blank
hard

Fill all three blanks to create a loop that prints fruits in uppercase only if their name length is less than 7.

Bash Scripting
for fruit in apple banana cherry date; do
  if [[ ${#fruit} [1] 7 ]]; then
    [2] "$fruit" | [3] '[:lower:]' '[:upper:]'
  fi
done
Drag options to blanks, or click blank then click option'
A<
Becho
Ctr
D>
Attempts:
3 left
💡 Hint
Common Mistakes
Using '>' instead of '<' for the length check.
Using 'cut' or other commands instead of 'tr' for case conversion.