egrep: empty (sub)expression : Unexpected behaviour for bash Loop on Mac
Solution 1:
The shell expands the subexpression $file
on the right hand side of the pipe before the variable has any value. So in both functions egrep
is actually called with the empty string in place of $file
. The first function doesn't throw an error because an empty pattern is a valid parameter for egrep
both on macOS and on Linux. In the second fucntion the difference comes into play because the empty subexpression causes BSD egrep
(the one used on macOS) to throw an error.
Example:
echo foo | egrep "|foo"
egrep: empty (sub)expression
as another example with $file
having no value
echo foo | egrep "$file|foo"
egrep: empty (sub)expression
So, as a workaround in your function use:
egrep -e "$file" -e 'OK'
I have no idea why you are using egrep
when you are looking for fixed strings.