String Operators

# Length ${#string}
= Equal [[ $a = $b ]]
!= Not equal [[ $a != $b ]]
> Greater than [[ $a > $b ]]
< Less than [[ $a < $b ]]
-z is Zero [[ -z $a ]]
-n is non Zero [[ -n $a ]]
str Assigned [[ $a ]]
#!/bin/bash
longer="The quick brown fox jumps over the lazy dog."
short="A shorter string"

#note don't use any whitespace within the following:
echo "The longer string has ${#longer} characters."
echo "The short string has ${#short} characters."

[[ $longer = $short ]] && echo "Equal" || echo "Not equal"
[[ $longer != $short ]] && echo "Not equal" || echo "Equal"
[[ $longer > $short ]] && echo "Longer" || echo "Shorter"
[[ $longer < $short ]] && echo "Shorter" || echo "Longer"
[[ -z $longer ]] && echo "Zero length" || echo "1 or more characters"
[[ -n $longer ]] && echo "1 or more characters" || echo "Zero length"
[[ $longer ]] && echo "String is not empty" || echo "Empty"

*note: above is using pseudo ternary one liner with && and || replacing normal ? and :

 

 

Substrings

 

The following tests whether a string contains substring:

#!/bin/bash
longer="The quick brown fox jumps over the lazy dog."
short="A shorter string"
test=fox

echo "The longer string is: $longer"
echo "The short string is: $short"
echo "The test string is: $test"

#here's a few variations to test if the substring is within the target string
[[ "${longer,,}" = *"fox"* ]] && echo "Yes (case insensitive), tested using wildcards" || echo "No"
[[ "${longer}" =~ "fox" ]] && echo "Yes, tested for fox using =~" || echo "No"
[[ "${longer}" =~ $test ]] && echo "Yes, same as above substituting 'fox' for a var" || echo "No"
[[ "${longer}" = *"fox"* ]] && echo "Yes, tested using wildcards" || echo "No"
[[ "${longer}" = "fox" ]] && echo "Yes" || echo "Not an exact match"
[[ "${test,,}" = "fox" ]] && echo "Yes (case insensitive), exact match" || echo "No"

 

Substring extraction

 

Extract from a position and from a position with length

 

${string:position}

${string:position;length}

#!/bin/bash
longer="The quick brown fox jumps over the lazy dog."
short="A shorter string"

#extract all characters starting from the 16th (offset) position:
echo "Extracting remaining characters from the longer string, starting from the 16th, gives: ${longer:16}"

#extract 3 characters (length) starting from the 16th (offset) position:
echo "Extracting 3 characters from the longer string, starting from the 16th, gives: ${longer:35:4}"

 

 

Substring removal

 

Remove shortest starting match.

If string starts with substring, delete the shortest part that matches the substring:

${string#*substring}

 

Remove longest starting match.

If string starts with substring, delete the longest match from string and return the rest:

${string##substring}

 

Remove shortest ending match.

If string ends with substring, delete the shortest match from the end of string and return the rest:

${string%substring*}

 

Remove longest ending match.

If string ends with substring, delete the longest match from the end of string and return the rest:

${string%%substring*}

 

#!/bin/bash
longer="The quick brown fox jumps over the lazy dog."
short="A shorter string"
echo "The longer string is: $longer"
echo "The short string is: $short"
echo "The test string is: $test"

echo "Remove shortest starting match: " ${longer#*quick}
#substituting string for var, simply to show it can be done
test=quick
echo "Remove shortest starting match: " ${longer#*$test}

echo "Remove longest starting match: " ${longer##*brown}
test=brown
echo "Remove longest starting match: " ${longer##*$test}

echo "Remove shortest ending match: " ${longer%over*}
test=over
echo "Remove shortest ending match: " ${longer%$test*}

echo "Remove longest ending match: " ${longer%%fox*}
test=fox
echo "Remove longest ending match: " ${longer%%$test*}

 

 

Substring replacement

 

Replace first occurrence of substring with replacement.

 

If replacement is null, substring is deleted from string.

${string/substring/replacement}

 

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

${string/#substring/replacement}

 

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

${string/%substring/replacement}

 

Replace all occurrences of substring.

${string//substring/replacement}

 

If sub is null, substring is deleted from variable.

 

#!/bin/bash
string="Ducati Julia Linux Turing Ducati Julia Linux Turing Ducati Julia Linux Turing"

echo "This test string purposely repeats the words: $string"}

echo "Replace (/remove) substring with null replacement from string: " ${string/Julia}
echo "Replace substring with replacement from string: " ${string/Julia/Angela}

#substituting with variables, simply to show it can be done
substring=Julia
replacement=Angela
echo "Replace substring with replacement from string: " ${string/$substring/$replacement}

echo ${string/#Ducati/Trimuph}
#again, using variables
substring=Ducati
replacement=Triumph
echo ${string/#$substring/$replacement}

echo ${string/%Turing/Penrose}
#again, using variables
substring=Turing
replacement=Penrose
echo ${string/%$substring/$replacement}

 

 

Substring extraction

 

Extract substring from string at position;

${string:position}

 

Extract length characters of substring from string at position;

${string:position:length}

 

#!/bin/bash
echo ${string:=This test string with some randon words: Ducati Julia Linux Turing}

echo "Extract substring from string at position: " ${string:42}

echo "Extract length characters of substring from string at position: " ${string:22:17}