Parameter Expansion (Advanced)
Perform powerful string manipulations using slicing, pattern trimming, and substitutions all within the shell.
Slicing and Trimming
Output
Click Run to execute your code
Note: These expansions are POSIX/Bash features; exact behavior can vary by shell.
Pro Tip: Use braces around variables (e.g.,
${name}) when concatenating to avoid ambiguity.Caution: Pattern matching uses globs, not full regex. Use character classes and
*, ? patterns.Common Mistakes
1) Expecting regex
Parameter expansion patterns are glob-like, not regular expressions.
2) Off-by-one slices
Remember slicing is zero-based: ${s:0:1} is the first character.
Exercise: Filename Parts
Task: Given path=/var/log/nginx/access.log, echo the directory and the basename without extension using parameter expansion only.
Output
Click Run to execute your code
Show Solution
echo "${path%/*}"; base="${path##*/}"; echo "${base%.*}"Summary
- Slice with
${var:off:len}. - Trim with
#/##,%/%%. - Substitute with
${var/old/new}and${var//old/new}.
What's Next?
Wrap up with best practices in Professional Practices.
Enjoying these tutorials?