How do I read the first line of a file using cat?

前端 未结 11 783
渐次进展
渐次进展 2021-01-29 22:20

How do I read the first line of a file using cat?

11条回答
  •  一向
    一向 (楼主)
    2021-01-29 22:35

    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.

提交回复
热议问题