Linux and Networking

Linux and Networking

4.26. Replacing a substring.

${str/substr/replacement}

 

The above command replaces the first match of the substring substring with the replacement string. Making the match from start to finish.

 

Example:

 

$stringMY=abcABC123ABCabc

$echo ${stringMY/abc/xyz}

xyzABC123ABCabc

 

${str//substr/replacement}

 

The above command replaces all matches of the substring substr with the replacement string.

 

Example:

 

$stringMY=abcABC123ABCabc

$echo ${stringMY//abc/xyz}

xyzABC123ABCxyz

 

${str/#substr/reempl}

 

If the string substr matches the first characters of the string str, it replaces substr with the string reempl.

 

Example:

 

$stringMY=abcABC123ABCabc

$echo ${stringMY/#abc/XYZ}

XYZABC123ABCabc

 

${str/%substr/reempl}

 

If the string substr matches the last characters of the string str, it replaces substr with reempl.

 

Example:

 

$stringMY=abcABC123ABCabc

$echo ${stringMY/%abc/XYZ}

abcABC123ABCXYZ

The Demeter Project