This is a copy of the following wonderful short and helpful site. Thanks.
http://bashcurescancer.com/10-steps-to-beautiful-shell-scripts.html
October 16th, 2007
Linux gurus don’t use cut, awk, and sed when they want to replace or strip out a portion if a variable. They use parameter substitution. You can learn parameter substitution in less than 2 minutes. Here is the ten things you need to know:
1. Shell variables can be specified like $var or this ${var}.
$ var='a.ads,fssd2342%asd234#@.,&%,sdfgsdfgas4352'
echo ${var}
a.ads,fssd2342%asd234#@.,&%,sdfgsdfgas4352
2. ${#var} is the length of the variable.
$ echo ${#var}
42
3. ${var:pos} substrings the variable starting at pos.
$ echo ${var:10}
2342%asd234#@.,&%,sdfgsdfgas4352
4. ${var:pos:len} substrings the variable starting at pos with a max length of len.
$ echo ${var:10:5}
2342%
5. ${var#pattern} strips pattern from the front or left hand side of the variable. This form is not greedy meaning it stops as soon as the pattern is matched. ${var##pattern} is the greedy form.
$ echo ${var#*,}
fssd2342%asd234#@.,&%,sdfgsdfgas4352
$ echo ${var##*,}
sdfgsdfgas4352
6. ${var%pattern} strips pattern from the back or right hand side of the variable. This form is not greedy meaning it stops as soon as the pattern is matched. ${var%%pattern} is the greedy form.
$ echo ${var%,*}
a.ads,fssd2342%asd234#@.,&%
$ echo ${var%%,*}
a.ads
7. ${var/pattern/replacement} replaces pattern with replacement once.
$ echo ${var/a/A}
A.ads,fssd2342%asd234#@.,&%,sdfgsdfgas4352
8. ${var//pattern/replacement} replaces pattern with replacement globally.
$ echo ${var//a/A}
A.Ads,fssd2342%Asd234#@.,&%,sdfgsdfgAs4352
9. ${var/#pattern/replacement} if variable beginning matches the pattern it is replaced with replacement.
$ echo ${var/#a./llll}
llllads,fssd2342%asd234#@.,&%,sdfgsdfgas4352
10. ${var/%pattern/replacement} if variable end matches the pattern it is replaced with replacement.
$ echo ${var/%352/llll}
a.ads,fssd2342%asd234#@.,&%,sdfgsdfgas4llll
——————————— Some additions from a friend Bernd —————————–
escape() {
local var=$1 value
eval value=\$$var
value="${value//./__}"
value="${value//-/___}"
eval $var=\$value
}
unescape() {
local var=$1 value
eval value=\$$var
value="${value//___/-}"
value="${value//__/.}"
eval $var=\$value
}
servername="${line##servername+( | )}"
escape servername