Server Fault is a question and answer site for system and network administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

Does anyone know of a simple one liner to read the first line of a file in bash?

share|improve this question
up vote 21 down vote accepted
read -r FIRSTLINE < filename

Same result as the other answers but faster because it doesn't spawn any process, as "read" is a built-in bash command.

share|improve this answer
good point, +1 for you – drAlberT Sep 18 '09 at 12:21
This performs much better than doing 'head -n 1'. I was reading the first line of 265 files and my time went from approximately 15 seconds to less than 1 second – Sebastian Nov 25 '14 at 14:32
head -1


share|improve this answer
FIRSTLINE=`head -n 1 filename`

Stores the line in a variable for later use (note the inverted apostrophes).

share|improve this answer
$(command) is an alternate form that avoid the use of back ticks, not even simple to be found on certain keyboards. – drAlberT Sep 18 '09 at 8:48
good answer, but I'm voting down because e-t172's answer is better. Read is intended for this, and it's built-in to bash, as he says. – Lee B Sep 18 '09 at 10:13
nah, voting down a "good answer" becouse it is not the best possible is not so polite don't you think? – drAlberT Sep 18 '09 at 13:16
Lee - Why not just upvote the one you like instead? Downvotes should only be given for factually incorrect information or off topic stuff. – MDMarra Sep 19 '09 at 2:53

head -n 1 should do the trick

share|improve this answer
awk 'NR == 1' /etc/passwd
share|improve this answer
this will scan full file, then return the first line, which is inefficient for large file. read -r is better. – Richard Mar 12 '12 at 18:24

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.