Read the first line without head
Read the first line of a file with read -r line < file, a builtin, instead of head -n 1. The shell does it without starting a process.
A script wants the first line of a file, such as a version string, and calls head -n 1 for it each time.
printf '1.4.2\nsecond line\n' > VERSION
read -r first < VERSION
echo "[$first]"
TIMEFORMAT='%3R seconds'
echo "head:"
time (for i in {1..5000}; do x=$(head -n 1 VERSION); done)
echo "read:"
time (for i in {1..5000}; do read -r x < VERSION; done)[1.4.2]
head:
7.108 seconds
read:
0.037 secondsread is a shell builtin, and it reads a single line from its standard input, which the redirection connects to the file. The -r option stops it treating a backslash as an escape, as the read builtin documentation says. No process starts, so the loop is faster by a large factor on this run (5,000 iterations, Debian 12 container, arm64 laptop).
read strips leading and trailing blanks unless you set IFS= for it, and it returns status 1 on a last line without a newline, although the variable is still set. Add || [[ -n $x ]] in a loop that must handle that. Only the first line is read, so the rest of the file is untouched.