Everyone should know about grep. Most of the time it’s used for testing the existence of a specific regex pattern somewhere. baselayout uses grep frequently for many tasks like so :-

grep-q '^envID:[/proc/self/status

Did you know that bash can do this without the use of grep? Here’s how …

    [[ $'\n'$(/proc/self/status) =~ $'\n''envID:[[:space:]([:space:]]*[1-9]')]*[](1-9]')]

(NOTE: everything that matches the regex is now in the array BASH_REMATCH)

One important point is that the file is read as one big string, so the ^ $ anchors don’t work in the same way as they now point to the start and end of the file. The work around is to match on \n instead. As such we always put one at the start of the file incase the match is on the first line.

Now, which is faster? Well, I ran all our existing is_foo_sys checks in /sbin/functions.sh 500 times in a row. grep was around 1 whole second slower than bash! :jawdrop:

That’s impressive, but then again I’ve already shown bash to be faster than some other C programs ;)

What I don’t know is how it performs on large files … I would guess that grep is faster as it can return once a match is found whereas bash has to read the entire file, but as all files checked should be quite small there shouldn’t be a problem :)