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?
Thanks in advance
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?
Thanks in advance
head
takes the first lines from a file, and the -n
parameter can be used to specify how many lines should be extracted:
line=$(head -n 1 filename)
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.
to read first line using bash, use read
statement. eg
read -r firstline<file
firstline
will be your variable (No need to assign to another)