📢 Too many exams? Don’t know which one suits you best? Book Your Free Expert 👉 call Now!


    Question

    Which command safely iterates over filenames that may

    contain spaces and newlines in a shell script?
    A for f in $(ls); do ... Correct Answer Incorrect Answer
    B for f in *; do ... Correct Answer Incorrect Answer
    C ls | while read f; do ... Correct Answer Incorrect Answer
    D for f in $(find . -name '*'); do ... Correct Answer Incorrect Answer
    E for f in \ls`; do ... Correct Answer Incorrect Answer

    Solution

    **for f in *expands to filenames preserving spaces;lsand command substitution break on whitespace/newlines. For arbitrary names including newlines,find -print0withwhile IFS= read -r -d ''is safest but among options,for f in *` is correct.

    Practice Next
    ask-question