There isn't a bug at all. What you are seeing is typical behaviour from the shell.
Very often, when you enter a piped command like that - the output file will be created by the shell interpreter BEFORE the first command is executed. So the shell will create the junk2 file before the first command is executed, so it is ready for
tee
to write to it.
So your command:
junk2 will usually be created BEFORE the
ls
command is executed. But it's not always guaranteed, as you saw with some of your results.
If you're doing something like the above, using the
ls
command - then to avoid this problem, it's usually best to create any output files OUTSIDE of the directory you're working in.
e.g.
Bash:
ls | wc -w | tee /path/to/junk2
Where /path/to/junk2 is a path that is
NOT the current working directory.
OR, if the file
MUST be in the current working directory, then you could do something like this:
Bash:
ls | \grep -vw junk2 | wc -w | tee junk2
In the above - if the output file IS created by the shell BEFORE the
ls
command is ran - we can remove it from the listing using
\grep
BEFORE piping it to tee, which writes the output of the previous commands to the file.
And the reason I'm using
\grep
instead of
grep
is to escape any aliases that might be set up in the users .bashrc. Very often
grep
will be aliased to use various options to add line numbers to it's output, or to colourise output, or to add other types of formatting. So by using
\grep
, we can be 100% sure that the output from grep will just be filenames that were piped in by the previous
ls
command.
Many Linux distros set up aliases in users default .bashrc files for common commands like ls, grep etc.
So if you want to be sure that you're getting exactly what you're asking for, with no extra guff - then prepend the command with a backslash and you can escape any aliases that have been set-up!
I hope that clears things up!