Getting String character by Character in bash

Sometimes you may need to get string character by character or need to looping through the string. In python programming language, you can do this job very easily.

See below example in python.

your_string="Hello"
for c in your_string :
        print (c)

This will give output as,

H
e
l
l
o


We can do same thing in bash shell scripting also,

see below bash shell scripting example.

#getting ord value of chars.
your_string="Hello"
echo $your_string | awk -v ORS="" '{ gsub(/./,"&\n") ; print }' | \
while read char
do
echo $char
done


Cheers.... ! Happy looping through the string !

0 comments:

Post a Comment

Ask anything about this Tutorial.