In this blog, Sharing code to get only unique Characters present in a string. Please do not get confused with my Previous Blog . Its a different topic all together. Consider a String “allabouttechnologies“, so the distinct or unique Characters of this String are “alboutechngis“.
str="alllabouttechnologies" ##Sample String
newstr='' ##Blank new String
n=${#str} ## Length of a String
i=0
while [[ $i -le $n ]] ##looping Character by Character
do
Char=${str:$i:1}
CharCntInNewStr=echo ${newstr}| grep ${Char} | wc -l ##Checking if Character already exists in new String
if [[ $CharCntInNewStr -eq "0" ]]
then
newstr=${newstr}${str:$i:1} ## appending Character if it is not present
fi
i=$(( $i + 1))
done
echo $newstr
![](https://www.allabouttechnologies.co.in/wp-content/uploads/2022/12/image-11.png)