When input “153”, the shell script will identify and display “1”, “5” and “3” on the screen sequentially. If input “69”, it will display “6” and “9”.
            Asked
            
        
        
            Active
            
        
            Viewed 92 times
        
    4 Answers
0
            
            
        Bash treats everything as a string.
Here is a Bash way to loop character by character over a string:
s=123
for (( i=0; i<${#s}; i++ )); do
    echo "${s:$i:1}"
done
Prints:
1
2
3
Still for you to do:
 
    
    
        dawg
        
- 98,345
- 23
- 131
- 206
0
            
            
        Another way of doing it:
#!/bin/bash
string="12345"
while IFS= read -n 1 char
do
    echo "$char"
# done <<< "$string"
done < <(echo -n "$string")
A here-string can be used instead of a < <(echo -n ...) construct if you do not care about a trailing newline.
 
    
    
        fpmurphy
        
- 2,464
- 1
- 18
- 22
 
     
     
    