#1 03 May 2009 06:51

Leo_Gutierrez
Member
Registered: 03 May 2009
Posts: 6

Separate string

Hi.
How can I separate this string:

"Leonardo"

like this:

L
e
o
n
a
r
d
o

I want to put each character in to a subscript of a array.
like this

array[0]=L
array[1]=e
array[2]=o
...

Thanks a lot.

Offline

#2 03 May 2009 07:26

Leo_Gutierrez
Member
Registered: 03 May 2009
Posts: 6

Re: Separate string

I found the solution:

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

Output:

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

leo@lein:~/Escritorio/bash$

Thanks.

Offline

Board footer

Powered by FluxBB