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
    – jsotola
    Mar 7 '20 at 0:19
  • that is a command line, not command .... what does the first command output?
    – jsotola
    Mar 7 '20 at 0:23
  • The final effect or how it is implemented?
    – rogerdpack
    Mar 7 '20 at 3:50
5

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.
    – Kamila
    Mar 10 '20 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?
    – terdon
    Mar 7 '20 at 15:17

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.