Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

The return code from 'grep' is not as expected on Linux

I'm struggling to see why the following is returning a code of 1.

echo 'Total' | grep -c No 0 

So "No" doesn't exists in "Total". But then looking up its return code I'm seeing it as 1.

echo $? 1 

Why is return code showing up as 1? Is there a way to get around this?

like image 921
difurious Avatar asked Feb 15 '17 13:02

difurious


People also ask

What is the return value of grep?

grep has return value (0 or 1) and output.

What does grep return if nothing is found?

Indeed, grep returns 0 if it matches, and non-zero if it does not. Hence my comment. In the shell 0 means success.

What is a return code in Linux?

An exit code, or sometimes known as a return code, is the code returned to a parent process by an executable. On POSIX systems the standard exit code is 0 for success and any number from 1 to 255 for anything else. Exit codes can be interpreted by machine scripts to adapt in the event of successes of failures.

Why does grep return a different exit code for each statement?

grep returns a different exit code if it found something (zero) vs. if it hasn't found anything (non-zero). In an if statement, a zero exit code is mapped to "true" and a non-zero exit code is mapped to false. So, you can use grep like this:

What does grep-X do in Linux?

It tells grep to match any one of the characters contained within the brackets “ [].” This means grep will match either “kB” or “KB” as it searches. Both strings are matched, and, in fact, some lines contain both strings. The -x (line regexp) will only match lines where the entire line matches the search term.

How do I make grep count in Linux?

We can make grep count for us in different ways. If we want to know how many times a search term appears in a file, we can use the -c (count) option. grep reports that the search term appears 240 times in this file. You can make grep display the line number for each matching line by using the -n (line number) option.

What does 1 mean in grep match?

So what you are seeing is the count of the match and not to be confused with the exit code of the grep match. The code 1 is because of no lines matching from the input. EXIT STATUS Normally the exit status is 0 if a line is selected, 1 if no lines were selected, and 2 if an error occurred. Show activity on this post.


2 Answers

According to man grep page, -c flag is for

-c, --count Suppress normal output; instead print a count of matching lines for each input file.

So what you are seeing is the count of the match and not to be confused with the exit code of the grep match. The code 1 is because of no lines matching from the input.

Have a look at the other case,

echo 'No' | grep -c No 1  echo $? 0 

Also to read on EXIT CODES on man grep page,

EXIT STATUS Normally the exit status is 0 if a line is selected, 1 if no lines were selected, and 2 if an error occurred.

like image 80
Inian Avatar answered Sep 17 '22 12:09

Inian


The exit code is 1 because nothing was matched by grep.

EXIT STATUS The exit status is 0 if selected lines are found, and 1 if not found. If an error occurred the exit status is 2. (Note: POSIX error handling code should check for '2' or greater.)

The output is zero because the count of 'Total' is zero. This due to the -c option:

-c, --count Suppress normal output; instead print a count of matching lines for each input file. With the -v, --invert-match option (see below), count non-matching lines. (-c is specified by POSIX.)

If you would like to force an exit code of 0, you can just append || true to your command:

echo 'Total' | grep -c No || true 
like image 34
rutgerm Avatar answered Sep 18 '22 12:09

rutgerm