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?
To look at the first few lines of a file, type head filename, where filename is the name of the file you want to look at, and then press <Enter>. By default, head shows you the first 10 lines of a file.
The bash first line in script begins with a shebang (#!). These are two mandatory characters, after which is indicated which program you are using for scripting. Now it is bin / bash, but other programs use this mechanism. If you did not specify the program, bash will be used by default.
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)
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)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With