How do I read the first line of a file using cat
?
I'm surprised that this question has been around as long as it has, and nobody has provided the pre-mapfile built-in approach yet.
IFS= read -r first_line
...puts the first line of the file in the variable expanded by "$first_line"
, easy as that.
Moreover, because read
is built into bash and this usage requires no subshell, it's significantly more efficient than approaches involving subprocesses such as head
or awk
.