Separate characters in a string

Bash shell questions
Post Reply
User avatar
MigrationUser
Posts: 336
Joined: 2021-Jul-12, 1:37 pm
Been thanked: 2 times
Contact:

Separate characters in a string

Post by MigrationUser »

03 May 2009 06:51
Leo_Gutierrez



Hi.
How can I separate this string:

Code: Select all

"Leonardo"
like this:

Code: Select all

L
e
o
n
a
r
d
o
I want to put each character in to a subscript of a array.
like this

Code: Select all

array[0]=L
array[1]=e
array[2]=o
...
Thanks a lot.

----------------------------

#2 03 May 2009 07:26
Leo_Gutierrez


I found the solution:

Code: Select all

#!/usr/bin/bash
name="Leonardo"
longitud=`expr length "$name"`
for i in $(seq 0 $longitud);
do
echo ${name:$i:1}
done
Output:

Code: Select all

leo@lein:~/Escritorio/bash$ bash shell.sh
L
e
o
n
a
r
d
o

leo@lein:~/Escritorio/bash$
Thanks.
Post Reply