Avoid cat in a pipe
Replace cat file | cmd with cmd < file. The measured cost of the extra process is small per call but adds up in a loop of thousands of calls.
cat file | wc -l is everywhere, and it starts one more process than the redirection does.
printf 'line\n%.0s' {1..1000} > f.txt
TIMEFORMAT='%3R seconds'
echo "cat pipe:"
time (for i in {1..2000}; do cat f.txt | wc -l >/dev/null; done)
echo "redirect:"
time (for i in {1..2000}; do wc -l < f.txt >/dev/null; done)cat pipe:
1.860 seconds
redirect:
0.856 secondsIn the pipe version the shell forks for cat and for wc, and wires them together with a pipe. The redirection opens the file in the shell itself and runs only wc. The shell grammar is in the pipelines section of the manual. The numbers are for 2,000 iterations in a Debian 12 container on an arm64 laptop, so they show the ratio and not what your machine does.
For one call the difference is a millisecond or less and not worth a style fight. The pipe is also clearer when the first command is a real producer. A file name that starts with a dash or holds an unquoted space is a bigger risk than the extra process, so quote it in both forms.