-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
  • 188,800
  • 56
  • 490
  • 992

4 Answers4

0

Pipe user input through sed:

echo $1 | sed 's/\(.\)/\1 /g'
Bohemian
  • 412,405
  • 93
  • 575
  • 722
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
  • 98,345
  • 23
  • 131
  • 206
0

Does your grep support the -o option? Then

$ echo "153" | grep -o .
1
5
3
Jens
  • 69,818
  • 15
  • 125
  • 179
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