Why is Gnu grep exit status always 0 on standard input?

Steve Newcomb

On a bash command line in Ubuntu 14.04 :

echo "z" | grep -e 'x' ; echo $PIPESTATUS

always displays 0, even though, obviously, there's no "x" in "z". But:

echo "z" > /tmp/z
grep -e 'x' /tmp/z ; echo $PIPESTATUS 

works as I expect, returning 1 if the pattern is 'x' (as shown), and 0 if the pattern is 'z'.

Why?

anubhava

PIPESTATUS is actually a BASH array variable containing a list of exit status values from the processes in the most-recently-executed foreground pipeline.

To print all the exit statuses, you need to use it as:

echo "z" | grep -e 'x' ; echo ${PIPESTATUS[@]}
0 1

Where 0 is exit status of first echo command and 1 is the exit status of grep command.

When you use echo $PIPESTATUS it just prints first element of array.

To check the grep command status you don't even need to use PIPESTATUS.

You should just use $?:

echo "z" | grep -q 'x' ; echo $?
1

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

grep always exits with 0 exit status in if statement

From Dev

Why is the exit status always 0 at the start of a script?

From Dev

Why is my GDB batch mode exit status always 0?

From Dev

Tee resets exit status is always 0

From Dev

Why does GNU make exit with 0 on errors?

From Dev

Why do I get different exit status for ps | grep in a script?

From Dev

Terminal - Why the exit command of grep is 0 even if a match is not found?

From Dev

ss exit code always 0

From Dev

frmcmp exit status should not be 0

From Dev

Testing grep exit status with pipe and variable unsuccessful

From Dev

Exit status of "git log --all --grep"

From Dev

supervisord always returns exit status 127 at WebFaction

From Dev

wait(status), WEXITSTATUS(status) always returns 0

From Dev

GNU grep check if input contains a LF character

From Dev

Why does `rake db:drop` have a 0 exit status, and raise no error, when it fails?

From Dev

Why I was confused "the exit status code is 0 when a command fails in BASH"

From Dev

curl shell command always returns error status 0 - even when fails on input errors

From Dev

exited: scrapy (exit status 0; not expected)

From Dev

Bash / exit status is 0, while expected is 255

From Dev

Tool to run a command until exit status is 0

From Dev

Why exit code 141 with grep -q?

From Dev

Why does this BSD grep result differ from GNU grep?

From Dev

Why does this BSD grep result differ from GNU grep?

From Dev

Why parentheses returns exit status but not braces

From Dev

Subprocess.check_output with grep: exit-status 2

From Dev

grep vs zgrep exit status differences with multiple files

From Dev

xmlhttp request status always returns 0

From Dev

xmlhttp request status always returns 0

From Dev

xmlhttprequest status is always 0 and no response in responseText

Related Related

  1. 1

    grep always exits with 0 exit status in if statement

  2. 2

    Why is the exit status always 0 at the start of a script?

  3. 3

    Why is my GDB batch mode exit status always 0?

  4. 4

    Tee resets exit status is always 0

  5. 5

    Why does GNU make exit with 0 on errors?

  6. 6

    Why do I get different exit status for ps | grep in a script?

  7. 7

    Terminal - Why the exit command of grep is 0 even if a match is not found?

  8. 8

    ss exit code always 0

  9. 9

    frmcmp exit status should not be 0

  10. 10

    Testing grep exit status with pipe and variable unsuccessful

  11. 11

    Exit status of "git log --all --grep"

  12. 12

    supervisord always returns exit status 127 at WebFaction

  13. 13

    wait(status), WEXITSTATUS(status) always returns 0

  14. 14

    GNU grep check if input contains a LF character

  15. 15

    Why does `rake db:drop` have a 0 exit status, and raise no error, when it fails?

  16. 16

    Why I was confused "the exit status code is 0 when a command fails in BASH"

  17. 17

    curl shell command always returns error status 0 - even when fails on input errors

  18. 18

    exited: scrapy (exit status 0; not expected)

  19. 19

    Bash / exit status is 0, while expected is 255

  20. 20

    Tool to run a command until exit status is 0

  21. 21

    Why exit code 141 with grep -q?

  22. 22

    Why does this BSD grep result differ from GNU grep?

  23. 23

    Why does this BSD grep result differ from GNU grep?

  24. 24

    Why parentheses returns exit status but not braces

  25. 25

    Subprocess.check_output with grep: exit-status 2

  26. 26

    grep vs zgrep exit status differences with multiple files

  27. 27

    xmlhttp request status always returns 0

  28. 28

    xmlhttp request status always returns 0

  29. 29

    xmlhttprequest status is always 0 and no response in responseText

HotTag

Archive