1

Just started learning UNIX so the question might seem really newbie but would appreciate the answer, as I've been trying to work it out on my own for an hour already with google's help, with no success however.

cat /etc/shadow 2>&1 | wc -l 

What would be the effect of this command? My guess is:

  1. The command prints the line count of “/etc/shadow”, if there is a standard error, it will be redirected to standard output and the error lines will be counted.
  2. The command prints the files of “/etc/shadow”, if there is a standard error, it will be redirected to standard output and the error's lines will be counted.
3
  • to help you with your research .... symbolhound.com Commented Mar 7, 2020 at 0:19
  • that is a command line, not command .... what does the first command output? Commented Mar 7, 2020 at 0:23
  • The final effect or how it is implemented? Commented Mar 7, 2020 at 3:50

2 Answers 2

4

X>&Y is for file descriptor redirection: this means that all output to fd X is actually going into Y. 2>&1 throws STDERR's output into STDOUT.


wc -l writes the number of input lines to STDOUT.


Together, the command cat /etc/shadow 2>&1 | wc -l returns the number of lines in /etc/shadow, as well as the number of error lines.

If you don't want to count those error lines, just use cat /etc/shadow | wc -l.

1
  • Thank you for your help! Really appreciated. Commented Mar 10, 2020 at 11:55
1

This is not a direct answer.

What is preventing you from doing some experimenting?

If you did experiments, then you may have answered your own question.

Run various combinations of the commands.

cat /etc/shadow cat /etc/shadows cat /etc/shadow 2>&1 cat /etc/shadows 2>&1 

and then pipe each one to wc

cat /etc/shadow | wc 

etc. etc.

1
  • Hi and welcome to the site. While this is helpful, it isn't actually providing an answer. Could you edit this so that you also answer the question as asked? Commented Mar 7, 2020 at 15:17

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.