Bash & Shell Scripting (50 Qs)•Part 2: Core Architecture & Implementation (Mid-Level)
What is Parameter Expansion in Bash (`${var:-default}`, `${var//search/replace}`)?
senior LevelSystem Design
In Simple Words:
Manipulates variables directly in memory without launching external processes (sed/awk):
- Default Value:
${VAR:-"fallback"}(uses fallback if$VARis unset or empty). - Assign Default:
${VAR:="default"}(assigns default to variable if empty). - Error if Unset:
${VAR:?"Variable is required!"}(aborts script if empty). - Substrings:
${VAR:0:5}(extracts first 5 characters). - String Replacement:
${VAR//foo/bar}(replaces all occurrences of "foo" with "bar"). - Strip Prefix/Suffix:
${filename%.*}: Strips file extension (archive.tar.gz->archive.tar).${path##*/}: Strips everything up to last slash (/etc/nginx/nginx.conf->nginx.conf).