Split a word into characters - Unix

If I have a word called "orange," how to divide it into separate characters.

My conclusion should be:

o r a n g e 
+4
source share
4 answers
 echo orange | fold -w 1 

Output

 o r a n g e 
+15
source

Here is a solution with grep :

echo "orange" | grep -o .

+2
source

Here is a solution using sed:

 echo orange | sed 's/./&\n/g' 
+1
source

A pure bash solution (faster than calling external tools on short lines):

 $ a="orange" $ [[ $a =~ ${a//?/(.)} ]] && printf '%s\n' "${BASH_REMATCH[@]:1}" o r a n g e 
+1
source

Source: https://habr.com/ru/post/1437495/


All Articles