Read characters from a text file using bash

advertisements

Does anyone know how I can read the first two characters from a file, using a bash script. The file in question is actually an I/O driver, it has no new line characters in it, and is in effect infinitely long.


The read builtin supports the -n parameter:

$ echo "Two chars" | while read -n 2 i; do echo $i; done
Tw
o
ch
ar
s

$ cat /proc/your_driver | (read -n 2 i; echo $i;)