Print 5 characters per line bash scripting

I said x no of char in the string.
How to print them 5 to 5?
let's say

$x = "abcdefghijklmnopqrstuvwxyz"

I wanted to print it like this and save it in a file

abcde
fghij
klmno
pqrst
uvwxy
z

any idea?

edited

part of my code

# data file
INPUT=encrypted2.txt
# while loop

while IFS= read -r -n1 char
do
        # display one character at a time
    echo "$char"
done < "$INPUT"
+4
source share
2 answers

You can use the command fold:

$ echo "abcdefghijklmnopqrstuvwxyz" | fold -w 5
abcde
fghij
klmno
pqrst
uvwxy
z
+8
source

:

grep -Po '.{5}|.*' <<< "abcdefghijklmnopqrstuvwxyz"
#or
grep -Po '.{1,5}' <<< "abcdefghijklmnopqrstuvwxyz" #@Cyrus

prints

abcde
fghij
klmno
pqrst
uvwxy
z

Or using a script

input=encrypted2.txt
while read -r -n5 ch
do
    echo "$ch"
done < "$input" >output.txt
+2
source

All Articles