User Tools

Site Tools


Sidebar

Programming Reference/Librarys

Question & Answer

Q&A is closed







bash:how_to:string_substitutions

bash: "How To" ... utilize string substitutions

string parameter substitution
operationwhatcodeexample using:
export TST='abcabcaa'
result from
(abcabcaa)
removefirst occurrence of substring 'xyz' in stringSTR=${STR/'xyz'}echo ${TST/'bc'}aabcaa
(abcabcaa)
substring 'xyz' everywhere in stringSTR=${STR//'xyz'}echo ${TST//'bc'}aaaa
(abcabcaa)
substring 'xyz' at very beginning of string (prefix)STR=${STR#'xyz'}echo ${TST#'abca'}bcaa
(abcabcaa)
substring 'xyz' at very end of string (suffix)STR=${STR%xyz}echo ${TST%'bcaa'}abca
(abcabcaa)
first n charactersSTR=${STR:n}echo ${TST:2}cabcaa
(abcabcaa)
last n charactersSTR=${STR:-n}echo ${TST::-3}abcab
(abcabcaa)
extractall characters after (!) position n
(first char position: n=0)
STR=${STR:n}echo ${TST:1}bcabcaa
(abcabcaa)
m characters after (!) position nSTR=${STR:n:m}echo ${TST:1:5}bcabc
(abcabcaa)
characters after (!) position n until end of string minus mSTR=${STR:n:-m}echo ${TST:1:-1}bcabca
(abcabcaa)
convertto upper caseSTR=${STR^^}echo ${TST^^}ABCABCAA
to lower caseSTR=${STR,,}echo ${TST,,}abcabcaa
cutfrom begin of string to first end of substring 'xyz'STR=${STR#*'xyz'}echo ${TST#*'bc'}abcaa
(abcabcaa)
from begin of string to last end of substring 'xyz'STR=${STR##*'xyz'}echo ${TST##*'bc'}aa
(abcabcaa)
from last occurrence of substring 'xyz' to end of stringSTR=${STR%'xyz'*}echo ${TST%'bc'*}abca
(abcabcaa)
from first occurrence of substring 'xyz' to end of stringSTR=${STR%%'xyz'*}echo ${TST%%'c'*}ab
(abcabcaa)

work in progress

on the occasion of the current invasion of Russia in Ukraine

Russian Stop this War
bash/how_to/string_substitutions.txt · Last modified: 2024/02/16 01:04 (external edit)

Impressum Datenschutz