I'm looking at a shell script that runs an nmap scan and saves matching lines to a variable:
RESULT=$(nmap -iL $(HOSTS) --open | grep "Nmap scan report|tcp open")
I'm confused by the `|` in the grep pattern. The nmap output doesn't contain a pipe character, so I'm not sure why it's there. Does it mean OR, and how is that different from the shell's `||` operator?
2 Answers
With regular `grep`—that is, without `-E` or `-P`—`|` is commonly used for alternation. In this command, it means the line should contain either `Nmap scan report` or `tcp open`. The shell’s `||` is different: it performs a logical OR between commands, while `|` is part of a regular-expression pattern passed to `grep`.
A clearer and more portable way to write this is to use extended regular expressions explicitly: `grep -E 'Nmap scan report|tcp open'`. In that mode, the plain `|` means alternation. The original `grep "...|..."` works with common GNU grep installations, but relying on that form can be confusing because basic and extended regular expressions use different syntax, and behavior may vary with non-GNU implementations.
So `-E` changes the regex flavor, letting the unescaped pipe mean OR. That makes the intent much easier to see.

That clears it up—thanks! I was mixing up shell operators with regular-expression syntax.