Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. 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

What the reason the number of lines differs?

$ head -n 100000 ./access.log > ./data/log.sample
$ cat $_ | wc -l
share|improve this question
Also visit Special Parameters for bash – Pandya Mar 23 at 10:30
In an interactive context, !$ does what you wanted, but it won't work in a script IIRC. – zwol Mar 23 at 12:18
@zwol: Yes. I asked a question about it. – Loom Mar 23 at 12:31
You should use the tee command in your script. – Diti Mar 24 at 7:33
up vote 26 down vote accepted

$_ is expanding to ./access.log (last argument of the last executed command), not ./data/log.sample.

So you are actually seeing the number of lines of ./access.log.

The redirection (>) is not part of the head command as it is done by the shell before the head command is even started. Hence with $_ you would get ./access.log.

From man bash:

($_, an underscore.) At shell startup, set to the absolute pathname used to invoke the shell or shell script being executed as passed in the environment or argument list. Subsequently, expands to the last argument to the previous command, after expansion. Also set to the full pathname used to invoke each command executed and placed in the environment exported to that command. When checking mail, this parameter holds the name of the mail file.

share|improve this answer
@J.Chomel I'm not sure what you think is a bug, even jokingly. – chepner Mar 23 at 20:14

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.