string parameter substitution | ||||
---|---|---|---|---|
operation | what | code | example using: export TST='abcabcaa' | result from (abcabcaa) |
remove | first occurrence of substring 'xyz' in string | STR=${STR/'xyz'} | echo ${TST/'bc'} | aabcaa (a |
substring 'xyz' everywhere in string | STR=${STR//'xyz'} | echo ${TST//'bc'} | aaaa (a |
|
substring 'xyz' at very beginning of string (prefix) | STR=${STR#'xyz'} | echo ${TST#'abca'} | bcaa ( |
|
substring 'xyz' at very end of string (suffix) | STR=${STR%xyz} | echo ${TST%'bcaa'} | abca (abca |
|
first n characters | STR=${STR:n} | echo ${TST:2} | cabcaa ( |
|
last n characters | STR=${STR:-n} | echo ${TST::-3} | abcab (abcab |
|
extract | all characters after (!) position n (first char position: n=0) | STR=${STR:n} | echo ${TST:1} | bcabcaa ( |
m characters after (!) position n | STR=${STR:n:m} | echo ${TST:1:5} | bcabc ( |
|
characters after (!) position n until end of string minus m | STR=${STR:n:-m} | echo ${TST:1:-1} | bcabca ( |
|
convert | to upper case | STR=${STR^^} | echo ${TST^^} | ABCABCAA |
to lower case | STR=${STR,,} | echo ${TST,,} | abcabcaa | |
cut | from begin of string to first end of substring 'xyz' | STR=${STR#*'xyz'} | echo ${TST#*'bc'} | abcaa ( |
from begin of string to last end of substring 'xyz' | STR=${STR##*'xyz'} | echo ${TST##*'bc'} | aa ( |
|
from last occurrence of substring 'xyz' to end of string | STR=${STR%'xyz'*} | echo ${TST%'bc'*} | abca (abca |
|
from first occurrence of substring 'xyz' to end of string | STR=${STR%%'xyz'*} | echo ${TST%%'c'*} | ab (ab |
work in progress