Bash Scripting - ArraysYou want to store a list of filenames in an array and print each filename on a new line. Which script correctly does this?Afiles=(file1.txt file2.txt file3.txt) for f in ${files[@]}; do echo $f; doneBfiles=[file1.txt, file2.txt, file3.txt] for f in $files; do echo $f; doneCfiles=(file1.txt file2.txt file3.txt) for f in $files; do echo $f; doneDfiles={file1.txt file2.txt file3.txt} for f in ${files[@]}; do echo $f; doneCheck Answer
Step-by-Step SolutionSolution:Step 1: Check array declarationfiles=(file1.txt file2.txt file3.txt) for f in ${files[@]}; do echo $f; done uses parentheses and space-separated items, which is correct.Step 2: Check loop syntaxfiles=(file1.txt file2.txt file3.txt) for f in ${files[@]}; do echo $f; done uses for f in ${files[@]} to loop over all items correctly.Final Answer:files=(file1.txt file2.txt file3.txt) for f in ${files[@]}; do echo $f; done -> Option AQuick Check:Correct array and loop syntax = files=(file1.txt file2.txt file3.txt) for f in ${files[@]}; do echo $f; done [OK]Quick Trick: Use ${array[@]} to loop all items [OK]Common Mistakes:MISTAKESUsing square brackets for arraysLooping over $files instead of ${files[@]}Using curly braces for array declaration
Master "Arrays" in Bash Scripting9 interactive learning modes - each teaches the same concept differentlyLearnWhyDeepVisualTryChallengeProjectRecallTime
More Bash Scripting Quizzes Arrays - Array length - Quiz 12easy Error Handling - Custom exit codes (exit N) - Quiz 11easy Functions - Calling functions - Quiz 7medium Functions - Return values (return and echo) - Quiz 15hard String Operations - Uppercase and lowercase conversion - Quiz 4medium String Operations - String length (${#var}) - Quiz 13medium Text Processing in Scripts - Here documents (<<EOF) - Quiz 12easy Text Processing in Scripts - sed for substitution in scripts - Quiz 7medium Text Processing in Scripts - awk field extraction in scripts - Quiz 12easy Text Processing in Scripts - cut and paste - Quiz 1easy