linux - read - shell get the first line of a file
Get first line of a shell command's output (1)
While trying to read the version number of
vim, I get a lot additional lines which I need to ignore. I tried to read the manual of
head and tried the following command:
vim --version | head -n 1
I want to know if this is the correct approach?
Yes, that is one way to get the first line of output from a command.
If the command outputs anything to standard error that you would like to capture in the same manner, you need to redirect the standard error of the command to the standard output stream:
utility 2>&1 | head -n 1
There are many other ways to capture the first line too, including
sed 1q (quit after first line),
sed -n 1p (only print first line),
awk 'FNR == 1' (only print first line) etc.