What is the exit status for grep in Bash?

What is the exit status for grep in Bash?

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. 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.

Why is the exit code 1 in grep?

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.

When to omit the exclamation mark in grep?

You’re almost there. Just omit the exclamation mark: The if condition is fulfilled if grep returns with exit code 0 (which means a match). The ! exclamation mark will negate this. Consistentecy and simple rules will help you debug and maintain scripts in a long run Thanks for contributing an answer to Unix & Linux Stack Exchange!

What is the exit status for grep in POSIX?

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.)

What to do if grep returns 0 in shell?

In shell scripts I suggest you to use the ”catch-1-grep“ (c1grep) utility function: grep can also return other codes if it’s interrupted by a signal (e.g. 130 for SIGINT). Since we only want to ignore exit status 1, we use test to suppress that specific exit status. If grep returns 0, test is not run.

Why does ps aux | grep always succeed in Bash?

This suggests that ps aux | grep -q “bla bla” was successful and created no output. It’s no surprise that grep -q creates no output. That’s what the -q is for. So therefore, your command must have had a true status, which implies that the grep did successfully find a match.

Why does grep-q always find a match?

It’s no surprise that grep -q creates no output. That’s what the -q is for. So therefore, your command must have had a true status, which implies that the grep did successfully find a match. We know that grep will always find a match in this case, because the list of processes from ps will include grep itself; the grep will always find itself.