bash - How to iterate through each letter in a string in Unix Shell -


this question has answer here:

i trying iterate through string taken input through read command. i'm trying output number of each letter , each letter should use loop output each letter in turn. example, if user enters "picasso", output should be:

letter 1: p letter 2: letter 3: c letter 4: letter 5: s letter 6: s letter 7: o

here current code:

#!/bin/bash  # prompt user enter word , output each letter in turn.   read -p "please enter word: " word  in $word  echo "letter $i: $word" done 

should placing input array? i'm still new programming loops i'm finding impossible figure out logic.

any advice? thanks.

combining answers dtmilano , patrat give you:

read -p "please enter word: " word  in $(seq 1 ${#word})  echo "letter $i: ${word:i-1:1}" done 

${#word} gives length of string.


Comments

Popular posts from this blog

javascript - jQuery: Add class depending on URL in the best way -

caching - How to check if a url path exists in the service worker cache -

Redirect to a HTTPS version using .htaccess -