Programming Reference/Librarys
Question & Answer
Q&A is closed
This is an old revision of the document!
string parameter substitution | ||||
---|---|---|---|---|
operation | what | code | example using: export TST='abcabcaa' | result from abcabcaa |
remove | first occurence of substring 'xyz' in string | STR=${STR/'xyz'} | echo ${TST/'bc'} | aabcaa |
substring 'xyz' everywhere in string | STR=${STR//'xyz'} | echo ${TST//'bc'} | aaaa | |
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 | |
first n characters | STR=${STR:n} | echo ${TST:2} | cabcaa | |
last n characters | STR=${STR:-n} | echo ${TST::-3} | 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 first end of substring 'xyz' to end of string | STR=${STR%“xyz'*} | echo ${TST%'bc'*} | abca | |
from first end of substring 'xyz' to end of string | STR=${STR%%'xyz'*} | echo ${TST%%'c'*} | ab |
work in progress