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
Post a Comment