Redirect the standard IO streams

  • Thread starter Thread starter ping83416
  • Start date Start date
P

ping83416

Guest
I can easily understand the statements like

1) bzip2 --version >/tmp/list_right 2>/tmp/list_error
2) bzip2 --version >/tmp/list 2>&1

But I really don't understand the statement below. Please help.

bzip2 --version 2>&1 < /dev/null | head -n1
 


I can easily understand the statements like

1) bzip2 --version >/tmp/list_right 2>/tmp/list_error
2) bzip2 --version >/tmp/list 2>&1

But I really don't understand the statement below. Please help.

bzip2 --version 2>&1 < /dev/null | head -n1
The command calls bzip2,
with the version option,
--version
redirects stderr (2) to stdout (1),
redirects stdin (0) from /dev/null (Not needed),
< /dev/null
pipes stdout to the head command,
with the number of lines option,
and outputs only the first line of the multi line version and copyright notice, to stdout.

On my system it displays,
$ bzip2 --version 2>&1 < /dev/null | head -n1
bzip2, a block-sorting file compressor. Version 1.0.6, 6-Sept-2010.
See this article on File Redirection for more information about the predefined stdin, stdout, and stderr files.
 

Members online


Latest posts

Top