Casual Thoughts

Martin Schürrer's Blog. My new blog is featurebranch.com

May 18

Solving “bash: grep: command not found” on OS X

I’ve moved my blog to a new domain, you can find this post at featurebranch.com

$ some script | grep something
bash: grep: command not found

If you are using a Mac with a German (or French, see below) keyboard layout and your typing is as sloppy as mine, chances are you are familiar with this error. You probably also know that deleting the space before grep fixes it. Why is this?

Option 7 corresponds to the | character. Option Space corresponds to the non breaking space which looks just like the regular space but is - as far as the shell is concerned - totally different. And obviously there’s no ‘ grep’ command on your system. And if you still hold down Option while pressing space… Guess what will happen… Now how do we fix it?

Check out the documentation on Mac OS X Key Bindings or simply make your ~/Library/KeyBindings/DefaultKeyBinding.dict file look like this (if it doesn’t exist create it, if there are already bindings in it just add the one from below)

{
  "~ " = ("insertText:", " ");
}

What does this do? Every time you press Option Space instead of inserting a non breaking space OS X now inserts a regular space. Problem fixed.

Update: Thanks to Julien Palmas for pointing out that this also applies to French keyboad layouts:
for french keyboards, the non breaking space is option+shift+space, and the “|” character is option+shift+L. So fast typing often leads to the same error