I was writing a simple script that checks the output of a command that if it displays a certain keyword. To see if it works, I was checking the command from the bash command line.
$ ls | grep -q foo $ echo $?
It displayed 1 or 0 depends on the command output and a grep parameter as I expected.
I get little lazy to type the command again, so I added | echo $?
at the end of the command line.
$ ls | grep -q foo | echo $?
Then regardless the command output, it always returns 0, even if the first part returns 1.
I guess this is normal behavior but I'd like to know why bash works this way.