Bash piping in OSX prompts command not found, sometimes
Asked Answered
J

4

17

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?

Jowers answered 1/1, 2012 at 17:33 Comment(1)
take a look at this other post in superuser superuser.com/questions/78245/…Spaceship
H
37

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

Hans answered 1/1, 2012 at 17:34 Comment(5)
hi, thanks for your reply. I was not sure what you meant with "erase and replace it", but I removed the space between the "|“ and “sort“ and it seems to be working. Could you explain why, though?Jowers
Because it's not a space, it only looks like one.Hans
You may not be typing it consistently (e.g. Option-Space looks like a space, but isn't), or cutting/pasting or using history from an earlier mistyped version.Undoing
I do this all the time. Mac keyboards (at least in some parts of the world) create the pipe-symbol with Alt+7 and immediately afterwards it's common to accidentally press Alt+Space, which causes the wrong character to be typed (not a space, but a hard-space or whatever they are called).Shilashilha
If you add "\xC2\xA0": " " to your ~/.inputrc bash treats non-breaking spaces as normal ones. I blogged about this in more detail.Davit
D
45

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 (if you are using bash). 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 kinds of issues), I wrote a blog post about it some time ago.

Davit answered 17/8, 2014 at 15:14 Comment(2)
Thanks @Michael I still run into this every so often.Jowers
Awesome answer +1Marceline
H
37

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

Hans answered 1/1, 2012 at 17:34 Comment(5)
hi, thanks for your reply. I was not sure what you meant with "erase and replace it", but I removed the space between the "|“ and “sort“ and it seems to be working. Could you explain why, though?Jowers
Because it's not a space, it only looks like one.Hans
You may not be typing it consistently (e.g. Option-Space looks like a space, but isn't), or cutting/pasting or using history from an earlier mistyped version.Undoing
I do this all the time. Mac keyboards (at least in some parts of the world) create the pipe-symbol with Alt+7 and immediately afterwards it's common to accidentally press Alt+Space, which causes the wrong character to be typed (not a space, but a hard-space or whatever they are called).Shilashilha
If you add "\xC2\xA0": " " to your ~/.inputrc bash treats non-breaking spaces as normal ones. I blogged about this in more detail.Davit
G
0

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

Gutty answered 25/1, 2017 at 16:44 Comment(0)
C
0

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- >
Caddish answered 19/7, 2018 at 18:12 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.