# grep's exit codes on valid match >0



## Seeker (Mar 16, 2011)

For example:

```
# cat /sys/conf/NOTES | grep -Em1 '^[a-z]+[[:blank:]]+[a-z]+$'
Exit code: [B]141[/B]
```
But there are also other examples, of various non zero exit codes, on success.

This caused me problems


----------



## SirDice (Mar 16, 2011)

Not according to grep(1):


> *DIAGNOSTICS*
> Normally, exit status is 0 if selected lines are found and 1 otherwise. But the exit status is 2 if an error occurred, unless the -q or --quiet or --silent option is used and a selected line is found.


----------



## kpa (Mar 16, 2011)

Your example returns zero on exit on my machine, RELEASE-8.2 i386. I'm using shells/bash as shell.


```
[kimmo@firewall ~]$ cat /sys/conf/NOTES | grep -Em1 '^[a-z]+[[:blank:]]+[a-z]+$'
device          loop
[kimmo@firewall ~]$ echo $?
0
[kimmo@firewall ~]$
```


----------



## Seeker (Mar 16, 2011)

SirDice said:
			
		

> Not according to grep(1):


I've read that.
Have you copied and executed my code, on command line? (Setup your shell, to show you exit codes of last commands)

I've received exit status of 2, when I've also received valid match, but that was because in dir there was an symlink that pointed to deleted file.

Here is another example (tcsh)

```
# cat /sys/conf/NOTES | grep -Em1 '^device'                  0 /root/sh_testground
device          hwpmc                   # Driver (also a loadable module)
blackhole#                                                         141 /root/sh_testground
```
When I remove *m1*, I get exit code 0

```
# cat /sys/conf/NOTES | grep -E '^device'
... lots of output ...
0 /root/sh_testground
```

All this on 8.1-RELEASE-p2


----------



## SirDice (Mar 16, 2011)

Yep, on tcsh I get 141 too.

Looking at the code of grep:

```
/* We register via atexit() to test stdout.  */
  exit (errseen ? 2 : status);
```

Status is actually the return from the grepfile function, which seems to return a count in some cases.


----------



## Seeker (Mar 16, 2011)

Lol_!_
Feature or a bug?!


----------



## Alt (Mar 16, 2011)

It can be some bits in $? pointing if program had coredump or not etc..
This regexp works good for me in csh (0,1,2 retcodes)


----------



## DutchDaemon (Mar 16, 2011)

sh, bash, and mksh -> 0
csh, tcsh -> 141


----------

