Page 1 of 1

Separate characters in a string

Posted: 2021-Jul-25, 12:15 pm
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.