I'm trying to get the return value from cpp according to How to invert a count returned from grep?. I've got a script failing with the message -bash: 1: command not found.

Here's the reduced test case:

$ echo `cpp -dM -fsanitize=undefined < /dev/null > /dev/null >2&1`
-bash: 1: command not found

Why is Bash trying to execute 1 as a command, and how do I clear the error?

closed as off-topic by roaima, garethTheRed, Archemar, dr01, cas Oct 27 '15 at 9:02

This question appears to be off-topic. The users who voted to close gave this specific reason:

  • "Questions describing a problem that can't be reproduced and seemingly went away on its own (or went away when a typo was fixed) are off-topic as they are unlikely to help future readers." – roaima, garethTheRed, Archemar, dr01, cas
If this question can be reworded to fit the rules in the help center, please edit the question.

  • 5
    You probably meant 2>&1 rather than >2&1 – steeldriver Oct 27 '15 at 6:19
  • @steeldriver - damn, you're right... Sorry about the extra noise, everyone. Do you want to answer and get the points? Or do you want me to delete the question? – user56041 Oct 27 '15 at 6:21
  • 2
    BTW, it's better to use $(cmd) than to put cmd in backticks. The $(cmd) form is easier to read, and can be nested. Also, it's not easy to put backticks in Stack Exchange comments. :) Also, doing echo $(cmd) (or the equivalent with backticks) is kinda pointless. Just do cmd. – PM 2Ring Oct 27 '15 at 6:23
  • @PM2Ring - thanks, I was not aware of that. I don't recall when I learned to use the back ticks, so I can't offer more information. I'm a C/C++ guy, and not a shell scripter (as you can probably tell :) – user56041 Oct 27 '15 at 6:26
  • No worries. The backticks / $(cmd) construction is when you want to capture the output of cmd so it can be put into a variable, or passed to another command. But if you just want to see the output in the terminal you should run cmd directly. And if you want to get the return code from the previous command it's in the $? special variable. – PM 2Ring Oct 27 '15 at 6:29

As @steeldriver pointed out, the problem is because of the mis-constructed io redirection. Here's why:

    cpp  > 2   &   1

The cpp process gets put in the background with output redirected to a file named "2", the ampersand is also a command separator like the semi-colon, so the next command in line is "1", which the shell cannot find.