-2

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”.

Martin Prikryl
  • 167,268
  • 50
  • 405
  • 846

4 Answers4

0

Pipe user input through sed:

echo $1 | sed 's/\(.\)/\1 /g'
Bohemian
  • 389,931
  • 88
  • 552
  • 692
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:

  1. Determine that s is a valid integer (Hint)
  2. Determine that s is a string representing less than 10,000? (Hint: inside of (( double_parentesis )) Bash treats those strings as integers)
dawg
  • 90,796
  • 20
  • 120
  • 197
0

Does your grep support the -o option? Then

$ echo "153" | grep -o .
1
5
3
Jens
  • 65,924
  • 14
  • 115
  • 171
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,403
  • 1
  • 17
  • 21