You can use
grep -E "(^|[[:blank:]])$var($|[[:blank:]])"
Or, assuming it is a GNU grep
(as suggested by Glenn Jackman):
grep -P '(?<!\S)\Q'"$var"'\E(?!\S)'
Choose the second one in case your $var
contains a literal text to search for and $var
can hold values containing special regex metacharacters like (
, )
, [
, {
, +
, ^
, etc., see What special characters must be escaped in regular expressions?
See an online demo:
s='hi helo tmp#100000 bye
100000 hi bye
hi 100000 bye'
var=100000
grep -E "(^|[[:blank:]])$var($|[[:blank:]])" <<< "$s"
# => 100000 hi bye
# hi 100000 bye
Here,
-E
enables the POSIX ERE syntax, -P
enables a PCRE syntax
(^|[[:blank:]])
- matches either start of input or a horizontal whitespace
(?<!\S)\Q
- (?<!\S)
checks if the char immediately on the left is a whitespace or start of string and \Q
starts quoting the pattern, the $var
will be parsed as a literal text
$var
- the var
contents
($|[[:blank:]])
- matches either end of input or a horizontal whitespace.
\E(?!\S)
- \E
stops quoting and (?!\S)
requires either a whitespace or end of string immediately on the right.