Bird
0
0

Given path="/home/user/docs/file.txt", which command extracts just the filename file.txt?

hard🚀 Application Q9 of 15
Bash Scripting - String Operations
Given path="/home/user/docs/file.txt", which command extracts just the filename file.txt?
Aecho ${path%%/*}
Becho ${path##*/}
Cecho ${path%/*}
Decho ${path#*/}
Step-by-Step Solution
Solution:
  1. Step 1: Understand ## operator removes longest prefix

    ${path##*/} removes everything up to the last slash.
  2. Step 2: Apply to '/home/user/docs/file.txt'

    Removing '/home/user/docs/' leaves 'file.txt'.
  3. Final Answer:

    echo ${path##*/} -> Option B
  4. Quick Check:

    Longest prefix removal with ##*/ = filename [OK]
Quick Trick: Use ##*/ to get filename from path [OK]
Common Mistakes:
MISTAKES
  • Using single # which removes shortest prefix
  • Using % which removes suffix instead
  • Confusing single and double operators

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Bash Scripting Quizzes