11

有时在我的终端(Ubuntu)上输入:

ls | grep toto

我收到此错误:

 grep: command not found*

请注意,shell 正在编写以空格为前缀的 grep。这怎么可能?

4

2 回答 2

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.

于 2013-11-15T12:51:22.400 回答
6

要完成 Gilles 的回答,您可以在 Ubuntu/Gnome 中禁用此行为,方法是将不可破坏的空格字符替换为键盘布局设置中的常用空格。

设置 -> 键盘 -> 布局 -> 选项,并选择“使用空格键输入不可破坏的空格字符”并将其设置为:“任何级别的常用空格”。

或者从命令行

setxkbmap -option "nbsp:none"

我在http://my.opera.com/nicomen/blog/unrecognized-character-xc2找到了这个修复。

这在 Perl 脚本的法语键盘上使用花括号或在 Python 中使用井号作为注释后跟一个空格时也很烦人。

于 2013-12-23T08:59:42.617 回答