How do you do search-and-replace, substrings, length and case conversion without external tools?
IntermediateConceptVariables, quoting and expansions
Answer
s="api-prod-api"
echo "${s/api/svc}" # svc-prod-api first match
echo "${s//api/svc}" # svc-prod-svc all matches
echo "${s/#api/svc}" # anchored at start
echo "${s/%api/svc}" # anchored at end
echo "${#s}" # 12 length
echo "${s:4:4}" # prod offset 4, length 4
echo "${s: -3}" # api last 3 (space before -)
echo "${s^^}" "${s,,}" # uppercase / lowercase (Bash 4+)
More on Variables, quoting and expansions
What are the rules for assigning variables in Bash?BeginnerWhat is the difference between single quotes, double quotes and `$'...'`?BeginnerWhy should you almost always quote variable expansions? Show what goes wrong.BeginnerExplain the difference between `"$@"`, `"$*"`, `$@` and `$*`.IntermediateWhat do `${var:-default}`, `${var:=default}`, `${var:+alt}` and `${var:?msg}` do?IntermediateHow do you strip prefixes and suffixes with parameter expansion?Intermediate