User Tools

Site Tools


Programming Reference/Librarys

Question & Answer

Q&A is closed


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

string parameter substitution
operationwhatcodeexample using:
export TST='abcabcaa'
result from
removefirst occurrence of substring 'xyz' in stringSTR=${STR/'xyz'}echo ${TST/'bc'}aabcaa
substring 'xyz' everywhere in stringSTR=${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 charactersSTR=${STR:n}echo ${TST:2}cabcaa
last n charactersSTR=${STR:-n}echo ${TST::-3}abcab
extractall characters after (!) position n
(first char position: n=0)
STR=${STR:n}echo ${TST:1}bcabcaa
m characters after (!) position nSTR=${STR:n:m}echo ${TST:1:5}bcabc
characters after (!) position n until end of string minus mSTR=${STR:n:-m}echo ${TST:1:-1}bcabca
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
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 stringSTR=${STR%'xyz'*}echo ${TST%'bc'*}abca
from first occurrence of substring 'xyz' to end of stringSTR=${STR%%'xyz'*}echo ${TST%%'c'*}ab

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