Skip to content

Instantly share code, notes, and snippets.

@leonowski
Created December 11, 2020 10:42
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save leonowski/4a2c6c5ef6273a974ed3d336c2af8082 to your computer and use it in GitHub Desktop.
Save leonowski/4a2c6c5ef6273a974ed3d336c2af8082 to your computer and use it in GitHub Desktop.
bash loop through string to find first recurring character
#!/bin/bash
#loop through characters in string taking input from first param
string=${1}
for (( i=0; i<${#string}; i++ )); do
#echo "${string:$i:1}"
for (( x=(($i + 1)); x<${#string}; x++ )); do
#echo XXX- "${string:$x:1}"
if [[ "${string:$i:1}" == "${string:$x:1}" ]]
then
echo "${string:$i:1}"
exit
fi
done
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment