Bird
0
0

Consider this script:

hard🚀 Application Q9 of 15
Bash Scripting - Quoting and Expansion
Consider this script:
files=$(ls /tmp)
count=$(echo "$files" | wc -w)
echo $count

What does this script output?
AAn error message
BNumber of lines in /tmp
CNumber of characters in /tmp
DNumber of files and directories in /tmp
Step-by-Step Solution
Solution:
  1. Step 1: Capture list of files in /tmp

    files=$(ls /tmp) stores all file names separated by spaces.
  2. Step 2: Count words in the list

    echo "$files" | wc -w counts the number of words, which equals number of files.
  3. Step 3: Output the count

    echo $count prints the number of files and directories in /tmp.
  4. Final Answer:

    Number of files and directories in /tmp -> Option D
  5. Quick Check:

    Word count of file list = Number of files [OK]
Quick Trick: Count words in file list to get number of files [OK]
Common Mistakes:
MISTAKES
  • Confusing word count with line or char count
  • Expecting error from echo with quotes
  • Misunderstanding command substitution output

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Bash Scripting Quizzes