String manipulation in Bash

I rely on Bash on much of my scripting needs although it is usually not worthy when you need to do something complicated. However, we usually think Bash limits are lower than they really are.

In this post I am going to explain the surprising number of string manipulation operations that Bash supports.

String Length

The syntax to know the lenght of a string is:


For example:

$ string=abcdefghijklmnopqrstuvwxyz
$ echo ${#string}

Substring Extraction


Extracts substring from $string at $position.

$ string=abcdefghijklmnopqrstuvwxyz
$ echo ${string:1}

Note that strings are zero-based indexed.

If you want to extract a substring of a specific length then:


Extracts $length characters from $string at $position.

For example:

$ echo ${string:5:3}

It is also possible to extract from the end of the string using a negative index value, but you have to be careful because of this little caveat:

$ echo ${string:-3}

will not print the last 3 characters of the string but this

$ echo ${string:-3}

This is because the following syntax:


sets the default shell variable value. So in our case ${string:-3} is actually doing nothing because string has already been set (if it hadn’t been set then its value would have been 3).

In order to avoid this behaviour you can use parentheses or an added space to escape the position parameter.

$ echo ${string:(-3)}
$ echo ${string: -3}

The position and length arguments can be parameterized, that is, represented as a variable, rather than as a numerical constant.

Substring Removal


Deletes shortest match of $substring from front of $string.


Deletes longest match of $substring from front of $string.

For example:

$ string=abcABC123ABCdef
$ echo ${string#a*C}
$ echo ${string##a*C}

To understand better the shortest and longest matches:

#      |----|       shortest
#      |----------| longest

Substrings can also be deleted from back of the string.


Deletes shortest match of $substring from back of $string.


Deletes longest match of $substring from back of $string.

$ echo ${string%B*C}
$ echo ${string%%B*C}

That means:

#                || shortest
#       |---------| longest

Substring Replacement


Replace first match of $substring with $replacement.


Replace all matches of $substring with $replacement.

$substring and $replacement may refer to either literal strings or variables.

$ string=abcABC123ABCabc
$ match=abc
$ new=xyz
$ echo ${string/$match/$new}
$ echo ${string//$match/$new}

We can also replace a string using # and % for front-end and back-end matching.


If $substring matches front end of $string, substitute $replacement for $substring.


If $substring matches back end of $string, substitute $replacement for $substring.

$ string=abcABC123ABCabc
$ match=abc
$ new=xyz
$ echo ${string/#$match/$new}
$ echo ${string/%$match/$new}

Of course, if you need to do something more complicated and you still want to solve it with a shell script you can always use sed or awk :)


speak up

Add your comment below, or trackback from your own site.

Subscribe to these comments.

Be nice. Keep it clean. Stay on topic. No spam.

You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>

*Required Fields