Linux and Networking

Linux and Networking

4.25. Deleting a substring.

Let’s look one by one at the different possibilities we have to delete a substring within a string:

 

${str#substr}

 

The above command removes the shortest match it finds from substr in str. Making the match from start to finish.

 

Example:

 

$stringMY=abcABC123ABCabc

$echo ${stringMY#a*C}

123ABCabc

 

Note: Note how the * is used as a wildcard (any character).

 

${str##substr}

 

The above command removes the longest match it finds from the substr in str. Making the match from start to finish.

 

Example:

 

$stringMY=abcABC123ABCabc

$echo ${stringMY#a*C}

abc

 

${str%substr}

 

The above command removes the shortest match it finds from substr in str. Making the match from the end to the beginning.

 

Example:

 

$stringMY=abcABC123ABCabc

$echo ${stringMY%b*c}

abcABC123ABCa

 

${str%%substr}

 

The above command removes the longest match it finds from the substr in str. Making the match from start to finish.

 

Example:

 

$stringMY=abcABC123ABCabc

$echo ${stringMY#b*c}

a

The Demeter Project