Ok, I'm seeing some weird behavior that I can't explain. If I run:
ssh server sh -c 'cd /var/tmp; pwd'

I get the user's home dir, But if I run:
ssh server sh -c 'echo foo; cd /var/tmp; pwd'

I get a blank line (?!?) followed by /var/tmp. I can replace the echo foo with pwd, and then both pwd's output the values expected.

Hmm, I wonder if this is related to cd, echo and pwd being built-ins, but from the local shell (no ssh), it works fine.

I'm seeing this from MacOS (Sequoia and Tahoe) to FreeBSD (multiple versions), MacOS (Sequioa) to MacOS (Sequioa), MacOS to Ubuntu 22.04.3 and Ubuntu 22.04.3 to Ubuntu 22.04.3.

So, looks like there's something more subtle about this. Since it means that bash has the same problem.

It almost like the first built-in is being ignored, but it's clearly not entirely ignored since echo is printing at least a new line, which makes this even more weird.

@mwl

@encthenet

The quotes are being handled by the local system.

The remote runs 'sh -c /var/tmp' followed by 'pwd'.

The cd command only happens in the subshell.

@mwl

@sab38
Winner winner!

Thanks, yeah, stupid shell quoting. I would say that's broken of ssh. It shouldn't be parsing the args when they are already broken out for you. Makes passing args safely very difficult.
@mwl