Bash Substring Tests

Testing Variables using the substring command

Substrings and Variables


A substring is basically a sequence of characters within a string. For example "am a subs" is subsection of the string "I am a substring". Bash gives you the ability to extract information from these strings. Below are some of the frequently used methods of extracting information:



Length of a String


We can retrieve the length of a given string by using the following: ${#string}.(Where string is the name of our variable). We can illustrate this easily with a simple script:



#!/bin/bash

test="Welcome to the Land of Linux"

echo "Our variable test is ${#test} characters long"

When we run the above script we will receive a value for the length of string. In this example, our sting "Welcome to the land of Linux" is 28 characters in length:



john@john-desktop:~/scripts$ ./test11.sh 
Our variable test is 28 characters long

Knowing the length of a string can be valuable when you need to validate an entry. You may ask for an entry to be a minimum of x characters and and have a Max size of y characters.


Extracting a Substring from a Variable


Another very useful feature within Bash is the ability to to extract specific sections of text from a variable. This can be achieved by using the following within your script:

${string:position} and ${string:position:length}

"string" specifies our variable name and the "position" is our starting point to extract information from. We can fine tune this further by using the "length" parameter. When length is used with position this means display information starting at position x for a length of y. We can illustrate this with a simple script:



#!/bin/bash

test="Welcome to the Land of Linux"

echo "Our variable test is ${#test} characters long"

test1=${test:0:7}
test2=${test:15:13}
test3=${test:0}

echo $test1
echo $test2
echo $test3

Output from the above script:



Our variable test is 28 characters long
Welcome
Land of Linux
Welcome to the Land of Linux

Notice our start position is "0" zero. This is the very first character of our string. Therefore if we look in position "0" for a length of 7 characters we will find the word "Welcome". Likewise the statement ${test:15:13} returns "Land of Linux". If we specify only ${test:0} we get the full string returned as position "0" is the starting position.


Strip shortest match of $substring from the front or back of a string


We can use the syntax of ${string#substring} to Strip the shortest match of $substring from front of $string

To strip the shortest match of $substring from back of $string we can use ${string#substring}

The "#" signifies the front and "% signifies from the back.


Strip the longest match of $substring from the front or back of a string


We can use the syntax of ${string##substring} to Strip the longest match of $substring from front of $string

To strip the longest match of $substring from the back of $string we can use ${string##substring}

The "##" signifies the front and "%% signifies from the back.

Example:



#!/bin/bash

test="land.of.linux"
echo "Stripping the shortest match from front:"
echo ${test#*.}

echo "Stripping the shortest match from back:"
echo ${test%.*}

echo "Stripping the longest match from front:" 
echo ${test##*.}

echo "Stripping the longest match from back:" 
echo ${test%%.*}

Output from the above script:



Stripping the shortest match from front:
of.linux
Stripping the shortest match from back:
land.of
Stripping the longest match from front:
linux
Stripping the longest match from back:
land


Replace FIRST match of $substring with replacement value


The following command allows you to replace the first match of the specified substring with a replacement value:

${string/substring/replacement}

Example:



#!/bin/bash

test="one.four.three"

echo "Before replacement: $test"

echo "After replacement: ${test/fo*./two.}"

Output from the above script:



john@john-desktop:~/scripts$ ./test15.sh 
Before replacement: one.four.three
After replacement: one.two.three

Replace ALL matches of $substring with replacement value


The following command allows you to replace all matches of the specified substring with a replacement value:

${string//substring/replacement}

Example:



#!/bin/bash

test="one two three one four one five"

echo "Before replacement: $test"

echo "After replacement: ${test//one/xxx}"

Output from the above script:



john@john-desktop:~/scripts$ ./test16.sh 
Before replacement: one two three one four one five
After replacement: xxx two three xxx four xxx five

index - Numerical position in $string of first character in $substring that matches


The following "index" command is useful as it allows you to find the first character within a substring. The index command will report back the position within the substring if a match is found or a "0" if no match was found:

The following syntax can be used:expr index "$string" $substring
The script below illustrates the basic use of the "index" command.



#!/bin/bash

test="AaBbCcDdEeFfGg"

testa=`expr index "$test" C`
testb=`expr index "$test" D`
testc=`expr index "$test" E`
testd=`expr index "$test" Z`

echo "testa is in position: $testa"
echo "testb is in position: $testb"
echo "testc is in position: $testc"
echo "testd is in position: $testd"

Output from the above script:



john@john-desktop:~/scripts$ ./test18.sh 
testa is in position: 5
testb is in position: 7
testc is in position: 9
testd is in position: 0

From the above output, we can see that the character "C" was found in position "5", however, no match was found for the character "Z" which resulted in a position of "0" being reported (not found).