Bash String Manipulation

1. Identify String Length inside Bash Shell Script
${#string}
2. Extract a Substring from a Variable inside Bash Shell Script
${string:position:length}
3. Shortest Substring Match
Following syntax deletes the shortest match of $substring from front of $string
${string#substring}
Following syntax deletes the shortest match of $substring from back of $string
${string%substring}
Following sample shell script explains the above two shortest substring match concepts.
$ cat shortest.sh
filename="bash.string.txt"
echo ${filename#.}
echo ${filename%.
}
$ ./shortest.sh
After deletion of shortest match from front: string.txt
After deletion of shortest match from back: bash.string
4. Longest Substring Match
Following syntax deletes the longest match of $substring from front of $string
${string##substring}
Following syntax deletes the longest match of $substring from back of $string
${string%%substring}
Following sample shell script explains the above two longest substring match concepts.
$ cat longest.sh
filename="bash.string.txt"
echo "After deletion of longest match from front:" ${filename##.}
echo "After deletion of longest match from back:" ${filename%%.
}
$ ./longest.sh
After deletion of longest match from front: txt
After deletion of longest match from back: bash
5. Find and Replace String Values inside Bash Shell Script
Replace only first match
${string/pattern/replacement}
It matches the pattern in the variable $string, and replace only the first match of the pattern with the replacement.
$ cat firstmatch.sh
filename="bash.string.txt"
echo "After Replacement:" ${filename/str./operations.}
$ ./firstmatch.sh
After Replacement: bash.operations.txt
Replace all the matches
${string//pattern/replacement}
It replaces all the matches of pattern with replacement.
$ cat allmatch.sh
filename="Path of the bash is /bin/bash"
echo "After Replacement:" ${filename//bash/sh}
$ ./allmatch.sh
After Replacement: Path of the sh is /bin/sh
Replace beginning and end
${string/#pattern/replacement}
Following syntax replaces with the replacement string, only when the pattern matches beginning of the $string.
${string/%pattern/replacement}
Following syntax replaces with the replacement string, only when the pattern matches at the end of the given $string.
$ cat posmatch.sh
filename="/root/admin/monitoring/process.sh"
echo "Replaced at the beginning:" ${filename/#\/root/\/tmp}
echo "Replaced at the end": ${filename/%.
/.ksh}
$ ./posmatch.sh
Replaced at the beginning: /tmp/admin/monitoring/process.sh
Replaced at the end: /root/admin/monitoring/process.ksh
6. Check if Two Strings are Equal
VAR1="Linuxize"
VAR2="Linuxize"
if [ "$VAR1" = "$VAR2" ]; then
echo "Strings are equal."
else
echo "Strings are not equal."
fi
7. Check if a String Contains a Substring
VAR=’GNU/Linux is an operating system’
if [[ $VAR == "Linux" ]]; then
echo "It’s there."
fi
Copy
8. Check if a String is Empty
VAR=”
if [[ -z $VAR ]]; then
echo "String is empty."
fi

Leave a Reply

Your email address will not be published. Required fields are marked *