I'm looking at a shell script that runs an nmap scan and stores selected lines in a variable:
RESULT=$(nmap -iL $(HOSTS) --open | grep "Nmap scan report|tcp open")
The scan output doesn't contain a literal backslash or pipe sequence, so I'm confused about why `|` is in the grep pattern. Does it mean OR? I thought shell OR was written as `||`, and I'm not sure whether the backslash is escaping the pipe character.
2 Answers
For portability, be aware that `|` is not handled identically by every grep implementation in basic-regex mode. A clearer and more portable choice is `grep -E "Nmap scan report|tcp open"`, where `|` explicitly means alternation in an extended regular expression. Also, `$(HOSTS)` only works if `HOSTS` is a command or function; if it’s meant to be a variable, it would normally be written as `$HOSTS` or `${HOSTS}`.
Yes—in the usual GNU grep implementation, `|` means alternation, or OR, when using grep’s default basic regular expressions. This pattern matches lines containing either `Nmap scan report` or `tcp open`. The backslash isn’t expected to appear in nmap’s output; it changes how grep interprets the pipe in the search pattern.

If you use extended regular expressions instead, write it more clearly as `grep -E "Nmap scan report|tcp open"`. The shell’s `||` is a different operator: it means logical OR between commands, not alternatives inside a regular expression.