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

Solution 1:

You don't need cat.

head -1 file

will work fine.

Solution 2:

You don't, use head instead.

head -n 1 file.txt

Solution 3:

There are many different ways:

sed -n 1p file
head -n 1 file
awk 'NR==1' file

Solution 4:

You could use cat file.txt | head -1, but it would probably be better to use head directly, as in head -1 file.txt.

Solution 5:

This may not be possible with cat. Is there a reason you have to use cat?

If you simply need to do it with a bash command, this should work for you:

head -n 1 file.txt