Tags
First set a parameter, param1
:
$ param1=hello
Now let’s see the result of parameter expansions
$ echo ${param1} # hello i.e., same as $param1)
$ echo ${param1}a # helloa
i.e., the braces separate the name
$ echo ${param2} # (nothing) i.e., nothing there
$ echo ${param2:-file*} # lists all files int the working directory starting with file.
$ echo ${param2:-$param1} # hello uses $param1's value...
$ echo $param2 #(nothing) ... but didn't change $param2
$ echo ${param3:=$param1} # hello assigns param1's value to param3
$ echo $param3 #hello
$ echo ${param1:2} # llo i.e., substring from 2
$ echo ${param1:2:2} # ll i.e., substring from 2, len 2
$ echo ${param1#he} # llo i.e., strip shortest match from start
$ echo ${param1#hel*} # lo i.e., strip shortest match from start
$ echo ${param1#he*l} # lo i.e., strip shortest match from start
$ echo ${param1##he*l}# o i.e., strip longest match from start
$ echo ${param1%l*o} # hel i.e., strip shortest match from end
$ echo ${param1%%l*o} # he i.e., strip longest match from end
$ echo ${param1/l/p} # heplo i.e., replace as few as possible
$ echo ${param1//l/p} # heppo i.e., replace as many as possible
$ echo ${!param*} # list parameter names starting with param
$ echo ${#param1} # 5 i.e., the length of param
Examples
1. Rename all .GIF files to .gif
$ for file in *.GIF; do mv $file ${file%GIF}gif; done
2. Number the files sequentially
$ cnt=0; for file in *.gif; do mv $file $cnt$file; let cnt=cnt++; done
3. Get rid of the numbers’
$ for file in *.gif; do mv $file ${file##[0-9]}; done
Reference: Directly copied from here with minor changes (as it was in a very-easy-to-understand form in the original).