1. Explain glob patterns and extended pattern matching in bash. How do they differ from regular expressions?
Glob patterns for pathname expansion: * matches zero or more characters, ? matches single character, [abc] matches one of a, b, c, [a-z] matches range, [!abc] matches any except a, b, c. Examples: *.txt matches all .txt files, file?.txt matches file1.txt but not file10.txt, [A-Z]*.txt matches uppercase-starting txt files. Extended globbing (enable with shopt -s extglob): ?(pattern) matches zero or one occurrence, *(pattern) matches zero or more, +(pattern) matches one or more, @(pattern) matches exactly one, !(pattern) matches anything except pattern. Examples: ?(*.txt) matches .txt files or nothing, +(digit).txt matches digit followed by one or more digits, !(*.txt) matches everything except .txt files. Brace expansion: {a,b,c} expands to a b c, {1..10} expands to 1 through 10, {a..z} expands to alphabet. Combine: file{1..3}.txt expands to file1.txt file2.txt file3.txt. Useful for batch operations: mkdir dir{1..5} creates five directories. Globs vs regex: globs for filename matching (pathname expansion), regex for text pattern matching. Globs: * means zero or more of any character. Regex: .* means zero or more of any character (* quantifier for previous atom). Globs: ? means one character. Regex: . means one character, ? makes previous optional. Regex in bash: [[ ]] supports regex with =~ operator: [[ $VAR =~ ^[0-9]+$ ]] matches VAR contains only digits. Capture groups: BASH_REMATCH[0] is full match, BASH_REMATCH[1] is first capture group. Example: ```bash if [[ $EMAIL =~ ^([a-zA-Z0-9._%+-]+)@([a-zA-Z0-9.-]+)\.([a-zA-Z]{2,})$ ]]; then echo "User: ${BASH_REMATCH[1]}" echo "Domain: ${BASH_REMATCH[2]}" fi ``` Use globs for file operations, regex for text validation and parsing. Understanding pattern matching enables powerful file selection and text processing.