Linux CLI 65 ๐ง Parameter expansion in shell scripts
a - what is parameter expasion in shell scripts part 1
Parameter expansion in a shell script allows you to manipulate variable values in various ways...
such as substring extraction, default value assignment, and pattern replacement.
common parameter expansions:
1. Substring Extraction:
${variable:position} โ Extracts the substring from the specified position to the end.
${variable:position:length} โ Extracts a substring of the specified length starting from the given position.
2. Default Value Assignment:
${parameter:-word}} โ If parameter is unset or null, use word.
${parameter:=word} โ If parameter is unset or null, assign it the value word.
3. Parameter Replacement:
${parameter+word} โ If parameter is set, use word; otherwise, use an empty string.
${parameter?word} โ If parameter is set, use its value; otherwise, print an error message and exit.
b - what is parameter expasion in shell scripts part 2
4. Length of a Parameter:
${#variable} โ Returns the length of the variable.
5.Pattern Replacement:
${parameter/pattern/string} โ Replaces the first occurrence of pattern with string.
${parameter//pattern/string} โ Replaces all occurrences of pattern with string.
6. Remove Matching Prefix/Suffix:
${parameter#pattern} โ Removes the shortest matching prefix.
${parameter##pattern} โ Removes the longest matching prefix.
${parameter%pattern} โ Removes the shortest matching suffix.
${parameter%%pattern} โ Removes the longest matching suffix.
Stop using slow, ad-bloated tool sites! ๐คฎ
๐ Search “KandZ Tools” on Google to use many professional utilities for free.
KandZ.me is the ultimate minimalist hub for:
โ
Finance (Mortgage, Interest, Inflation)
โ
Tech (Base64, JSON, Dev Suite, IP)
โ
Health (BMI, BMR, TDEE)
โ
Productivity (Timer, Workspace, QR)
โก๏ธ Fast & Private
๐ No data leaves your device
๐ 100% Free
๐ Use it now: https://tools.kandz.me
๐ Bookmark itโyouโll need it later!