Bash Script String Manipulation

When working with a string of characters, Bash Script has built in functionality that allows for them to be easily manipulated.

When working with the terminal, a string can be displayed using the ‘echo’ command, either with or without quote marks around the string.

echo Hello World!
echo "Hello World!"

It should be noted that single quotes will also work where no variables are incorporated into the string. Double quotes are necessary where an apostrophe is included in the string.

If it is necessary to incorporate the value of a variable into this message it can be done by including a dollar sign before the variable name.

FNAME=Fred
SNAME=Bloggs

echo "Hello $FNAME $SNAME!"

This will display the message, 'Hello Fred Bloggs!' in the terminal.

This syntax can also be used to populate a variable.

FNAME=Fred
SNAME=Bloggs

MESSAGE="Hello $FNAME $SNAME!"

echo "$MESSAGE"

The same message is displayed as before in the terminal.

It is also possible to add to a string using the '+=' symbols, so a further way of constructing this message is as follows.

FNAME=Fred
SNAME=Bloggs

MESSAGE="Hello "
MESSAGE+="$FNAME "
MESSAGE+="$SNAME!"

echo "$MESSAGE"

There is a second way of incorporating a variable into a string, which is very similar, but includes curly brackets around its name.

FNAME=Fred
SNAME=Bloggs

MESSAGE="Hello ${FNAME} ${SNAME}!"

echo "${MESSAGE}"

This is a more powerful way of incorporating a variable into a larger string, as can be seen in the following sections.

String Length

In order to find the length of a string, a hash symbol can be placed just before the variable name, inside the curly brackets.

FNAME=Fred
SNAME=Bloggs

MESSAGE="Hello ${FNAME} ${SNAME}!"

echo "The MESSAGE variable length is ${#MESSAGE} characters."

This displays the length as part of a larger string.

The MESSAGE variable length is 18 characters.

Index Value and Substring Functionality

Every character in a string has an index value, which can be used to reference it. The first character has an index value of zero, the second has an index of one and so on. This index value can be used to return a small portion of a larger string.

The substring functionality is incorporated after the variable name, but before the closing curly bracket. Below are examples of how to extract a portion of a string from the start, middle and end.

FNAME=Fred
SNAME=Bloggs

MESSAGE="Hello ${FNAME} ${SNAME}!"

echo "${MESSAGE:0:5}"
echo "${MESSAGE:6:4}"
echo "${MESSAGE:(-7)}"

The substring functionality starts with a colon. What follows depends on what portion of the larger string is required. To return a portion of the string from the start, the colon is followed by a zero, then another colon, followed by the number of characters required, as shown in the first example above.

To return the middle portion of the string, the index position of the first desired character is included after the colon, followed by another colon, and again, the number of characters required. This is shown in the second example above.

Finally, to extract a number of characters from the end of the string, a minus value is included after the colon in brackets, for example minus seven, to return the last seven characters, as shown in the last example above.

These examples will produce the following output.

Hello
Fred
Bloggs!

Replace and Remove Functionality

If desired, it is possible to replace part of a string with another value.

FNAME=Fred
SNAME=Bloggs

MESSAGE="Hello ${FNAME} ${SNAME}!"

echo "${MESSAGE}"
echo "${MESSAGE/Fred/Joe}"

Here, the word 'Fred' in the message 'Hello Fred Bloggs!' is replaced with 'Joe'.

Hello Fred Bloggs!
Hello Joe Bloggs!

It should be noted that this will only replace the first instance of the word 'Fred' in the string. In order to replace all instances, two forward slashes must precede the word 'Fred'.

echo "${MESSAGE//Fred/Joe}"

This same functionality can be used to remove part of a string.

FNAME=Fred
SNAME=Bloggs

MESSAGE="Hello ${FNAME} ${SNAME}!"

echo "${MESSAGE}"
echo "${MESSAGE/ Bloggs/}"

With the above example, a space, followed by the word 'Bloggs' is removed from the message.

Hello Fred Bloggs!
Hello Fred!

If for some reason all spaces need to be removed, this can be achieved in a similar fashion.

FNAME=Fred
SNAME=Bloggs

MESSAGE="Hello ${FNAME} ${SNAME}!"

echo "${MESSAGE}"
echo "${MESSAGE// /}"

Note the double slash before the space to remove all spaces. The resulting output is as follows.

Hello Fred Bloggs!
HelloFredBloggs!

It is also possible to use wildcards to replace and remove parts of a string.

FNAME=Fred
SNAME=Bloggs

MESSAGE="Hello ${FNAME} ${SNAME}!"

echo "${MESSAGE}"
echo "${MESSAGE/ */ Joe Smith!}"
echo "${MESSAGE/ */}"

Here, the star wildcard is used to denote all text after the space. In the first wildcard example, the first space and all text after it is replaced with ' Joe Smith!'. The second example just removes the first space and all text after it.

Hello Fred Bloggs!
Hello Joe Smith!
Hello