Sometimes I hate computers. Can someone explain to me why `ssh host pgrep -af pgrep` returns nothing, while`ssh host pgrep -af pgrep '2>&1'` (note the quotes) matches itself? And why this only happens if the remote host is running bash?
3
1
13
I _think_ what is happening is that ssh "guesses" that you wanted a shell on the other side when it sees special characters in the command, and only in that case runs bash -c, otherwise it just fork+execs. But that would be insane, right?
1
5
This seems to be supported by running `ssh host pstree` vs `ssh host pstree '|cat'`. The first has `sshd -> pstree`, second has `sshd -> bash -> pstree` (thanks Jeb!). Who decided this was a good idea, and how do I turn it off? :p
1
2
It looks like it is probably _not_ sshd doing this. It's exec code (github.com/openssh/openssh-pโ€ฆ) _always_ invokes the shell. Does bash detect whether it can get away with just exec'ing?
2
4
Well look-y here: github.com/bminor/bash/blob/โ€ฆ. Looks like this is an optimization in bash, where it avoids forking and just execs instead if it deems it possible! I guess that explains it... What a rabbit hole..
2
1
16
Replying to @jonhoo
ssh lacks an argv type invocation packet IIRC, so there's quoting hell going on too

Apr 15, 2020 ยท 5:47 PM UTC