11

Sometimes on my terminal (Ubuntu) when I type:

ls | grep toto

I get this error:

 grep: command not found*

Notice that the shell is writing grep prefixed by a space. How can this be possible?

Pandu
  • 1,606
  • 1
  • 12
  • 23
user2854544
  • 851
  • 1
  • 11
  • 23

2 Answers2

28

<Checks the source of your original question>

<pre style="width:650px; white-space:pre-wrap">Sometimes on my terminal (Ubuntu) when I type :

ls |&#160;grep toto

Thank you for copy-pasting the actual line! (But you didn't copy-paste the error message, naughty you!) See the problem? You have an unbreakable space after the pipe symbol. Shells only understand ASCII characters; all non-ASCII characters, including U+00A0 NO-BREAK SPACE, are treated as word constituents, so that unbreakable space is treated as part of the word that's in command name position.

You're presumably using a keyboard layout where you need to hold down AltGr to type |. Make sure to release the AltGr modifier so as not to accidentally type AltGr+Space instead of Space. Note that you don't need a space there, you can type ls |grep toto if that's easier on your fingers.

Community
  • 1
  • 1
Gilles 'SO- stop being evil'
  • 104,111
  • 38
  • 209
  • 254
6

To complete Gilles answer, you can disable this behavior in Ubuntu/Gnome by replacing non-breakable space character by usual space in keyboard layout settings.

Settings -> Keyboard -> Layout -> Options, and select "Using space key to input non-breakable space character" and set it to: "Usual space at any level".

Or from the command line

setxkbmap -option "nbsp:none"

I found this fix in http://my.opera.com/nicomen/blog/unrecognized-character-xc2.

This was also annoying on French keyboard for Perl scripts when using curly brackets or in Python when using hash sign for comments followed by a space.

Andre Miras
  • 3,580
  • 44
  • 47
  • Also https://stackoverflow.com/questions/8694442/bash-piping-in-osx-prompts-command-not-found-sometimes which has a nice alternative workaround which doesn't depend on X. – tripleee Nov 14 '17 at 13:00