How to get the first line of a file in a bash script?

前端 未结 6 1826
不思量自难忘°
不思量自难忘° 2020-12-04 06:59

I have to put in a bash variable the first line of a file. I guess it is with the grep command, but it is any way to restrict the number of lines?

6条回答
  •  挽巷
    挽巷 (楼主)
    2020-12-04 07:34

    line=$(head -1 file)
    

    Will work fine. (As previous answer). But

    line=$(read -r FIRSTLINE < filename)
    

    will be marginally faster as read is a built-in bash command.

提交回复
热议问题