What does `|` mean in this grep command?

August 8, 2026 2 answers
Asked by dotted.line
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.

Answers

+use the side entrance
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.

1st aid refresher: 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.

Reply to this answer
Transit nerd!
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}`.
Reply to this answer

Add your answer

Answers are reviewed before publication.

Related Discussions