Bash can be used to perform some basic string manipulation. It is best to put these to use when the logic does not get overly complicated. In this post we will look at some useful and commmonly used string manipulation technques that should come in handy in our every day scripting tasks.

String manipulation with Bash

Concatinate strings

Concatinate two strings variables x and y.

$ x="Unix"
$ y="Utils"
$ echo $x$y

UnixUtils

Delimit string with character

Delimit string with character and convert them into separate variables.

$ x='Unix-Utils-World'
$ IFS=- read -r x y z <<< "$x"
$ echo $x

Unix

$ echo $y

Utils

$ echo $z

World

Delimit string with character and convert it into an array

$ x='Unix-Utils-World'
$ IFS=- read -ra string <<< "$x"
$ echo ${string[@]}

Unix Utils World

$ echo ${string[0]}

Unix

$ echo ${string[1]}

Utils

$ echo ${string[2]}

World

Get Length of string

$ name=unixutils
$ echo ${#name}

9

Get substring from a specific position

$ name=unixutils
$ echo ${name:0}

unixutils

$ echo ${name:1}

nixutils

$ echo ${name:3}

xutils

$ echo ${name:4}

utils

Get substring from a given start position to a given end position

$ name=unixutils
$ echo $name

unixutils

$ echo ${name:0:4}

unix

$ echo ${name:0:7}

unixuti

Note that, with the above example one can get the first 2 characters from string by setting the start and end positions as 0 to 2, respectively.

$ str="unixutils"
$ echo ${str:0:2}

un

For example, to get the last two characters, it can be done by the following method that uses negative numbers for positional arguement.

$ str="unixutils"
$ echo ${str:(-2)}

ls

Replace one substring with another, in a string

$ x='Unix-Utils-World'
$ echo ${x/World/website}

Unix Utils website

$ echo ${x/World/'website welcomes you!'}

Unix Utils website welcomes you!

Delete substring from string

$ str='Unix-Utils-World'
$ echo $str

Unix-Utils-World

$ echo ${str/-World}

Unix-Utils

Remove all occurences of a substring in a string

In the below two examples, the first one removes only the first occurence of the substring, but the second examples removes all occurences of a substring.

$ str="unix-utils-world"
$ echo ${str/-}

unixutils-world

$ echo ${str//-}

unixutilsworld

Remove substring only if its a prefix to string

$ str="-unix-utils-world-"
$ echo ${str/#-}

unix-utils-world-

Remove substring only if its a suffix to string

$ str="-unix-utils-world-"
$ echo ${str/%-}

-unix-utils-world
 

Check if substring exists in string

string='UnixUtils welcomes you'
if [[ $string = *"welcomes you"* ]]; then
  echo "substring found!"
fi

substring found!

Convert string to uppercase

$ str="unixutils"
$ echo $str

unixutils

$ echo ${str^^}

UNIXUTILS

Convert string to lowercase

$ str="UnixUTILS"
$ echo $str

UnixUTILS

$ echo ${str,,}

unixutils

Convert the first character to upperase

$ string="unixutils"
$ echo ${string^}

Unixutils

Convert the first character to lowercase

$ string="Unixutils"
$ echo ${string,}

unixutils

Convert only certain characters to uppercase

$ string="unixutils"
$ echo ${string^^[u]}

UnixUtils

$ echo ${string^^[ui]}

UnIxUtIls

Convert only certain characters to lowercase

$ string="UNIXUTILS"
$ echo ${string,,[U]}

uNIXuTILS

$ echo ${string,,[US]}

uNIXuTILs

Bash can be used to manipulate strings when the requirements are simple. However when things get complicated, such as to work on complex patterns and logic, bash does not fair well. In such cases very sophisticated and commonly used data manipulation ‘awk’ is prefered.

This site uses Akismet to reduce spam. Learn how your comment data is processed.