dylanaraps / pure-sh-bible

📖 A collection of pure POSIX sh alternatives to external processes.
MIT License
6.45k stars 281 forks source link

Iterate over characters in a string #17

Open spiralofhope opened 4 years ago

spiralofhope commented 4 years ago

Drawn from https://stackoverflow.com/a/51052644/1190568

string='example'
__="$string"
while [ -n "$__" ]; do
  # All but the first character of the string
  rest="${__#?}"
  #  Remove $rest, and you're left with the first character
  first_character="${__%"$rest"}"
  echo "$first_character"
  __="$rest"
done