Bash piping in OSX prompts command not found, sometimes

谁说胖子不能爱 提交于 2019-11-27 12:27:58

问题


In the OSX terminal :

du -h | sort -nr
-bash:  sort: command not found

which sort
/usr/bin/sort

The weird thing is: I tried reproducing the error and it seems to be totally random. My PATH echoed:

/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/bin:/usr/X11/bin:/usr/local/git/bin:/usr/texbin:/Users/sytycs/.rvm/bin

This only occurs when piping and happens with grep, more, less etc. Any idea what is causing this?


回答1:


That space is not a space. Erase and replace it.




回答2:


This likely happens because you use a keyboard layout with a non-US layout (happened to me too). On German layouts, the pipe is typed with Alt+7, so chances are high that you press Alt+Space afterwards, which will produce a non-breaking space.

A quick solution: Add the line

"\xC2\xA0": " "

to your ~/.inputrc. This will map non-breaking spaces to normal ones which should solve the problem.

If you want more detail (or if you are interested in how you can track down these kind of issues, I wrote a blog post about it some time ago).




回答3:


The trick with ~/.inputrc doesn't work for zsh. But here you can configure iTerm to send a space when you type alt+space, for instance




回答4:


Bash is so sensitive to space when you are piping. Remove them all. I was facing the same problem by running this command:

|awk '{$1=$1};1' |  tr '[:upper:]' '[:lower:]' |  sort | uniq 
| awk '{ print length, $0 }' | sort -n -s | cut -d" " -f2- > dest_file

the errors were as here:

zsh: command not found:   tr
zsh: command not found:   sort

then I removed all the spaced and it got resolved:

|awk '{$1=$1};1'|tr '[:upper:]' '[:lower:]'|sort|uniq|awk '{ print length, $0 
}'|sort -n -s|cut -d" " -f2- >


来源:https://stackoverflow.com/questions/8694442/bash-piping-in-osx-prompts-command-not-found-sometimes

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!