JohnnyF
JohnnyF

Reputation: 1101

How to split a string into individual characters

I am trying to build a script that will print on screen each char from the given string.

Lets say I call the script show_chars then

./show_chars bkob bk.is

will give me

b
k
o
b

b
k
.
i
s

Upvotes: 2

Views: 54

Answers (2)

Gerard Rozsavolgyi
Gerard Rozsavolgyi

Reputation: 5064

or

echo $1 | grep -o .

works also

Upvotes: 3

anubhava
anubhava

Reputation: 785128

You can use fold for this:

s='bkob bk.is'
fold -w1 <<< "$s"
b
k
o
b

b
k
.
i
s

As per man fold:

  -w, --width=WIDTH
          use WIDTH columns instead of 80

Upvotes: 4

Related Questions