command executed via `ssh machine bash -c "..."` does not have access to /Volumes

The simple test case is this:

@max[git:master]$ /usr/bin/ssh max ls /Volumes
Macintosh HD
TM2
me
me9
@max[git:master]$ 

and

@max[git:master]$ /usr/bin/ssh max /bin/bash -c "ls /Volumes"
Applications
Calibre Library
Desktop
...

The latter is NOT doing an ls of /Volumes/ but of my $HOME directory.

This is a recent change. I've had a script that is > 10 years old that just started failing.

Here's another look:

@max[git:master]$ /usr/bin/ssh max /bin/bash -c "cd /Volumes && pwd"
/Users/layer
@max[git:master]$ echo $?
0
@max[git:master]$ 

It's weird that it just silently ignores the cd.

This is on

ProductVersion:		15.3
BuildVersion:		24D60
Answered by dklayer in 823483022

OK, decided to try quoting differently:

@max[git:master]$ ssh max bash -c '"ls /Volumes"'
Macintosh HD
TM2
me
me9
@max[git:master]$ 

So maybe the quoting rules changed?! As I said this script is > 10 years old.

Accepted Answer

OK, decided to try quoting differently:

@max[git:master]$ ssh max bash -c '"ls /Volumes"'
Macintosh HD
TM2
me
me9
@max[git:master]$ 

So maybe the quoting rules changed?! As I said this script is > 10 years old.

I think I understand the current behaviour. So why was it different before? One thing that has changed is the switch from bash to zsh as the default shell - but that shouldn’t matter if you were explicitly invoking /bin/bash. I don’t believe that the bash or ssh behaviour has changed. Mysterious.

Re this example:

/usr/bin/ssh max /bin/bash -c "ls /Volumes"

That runs this command on the remote machine (note no quotes):

/bin/bash -c ls /Volumes

According to man bash, the effect of bash -c is that “commands are read from the first non-option argument command_string. If there are arguments after the command_string, the first argument is assigned to $0 and any remaining arguments are assigned to the positional parameters. The assignment to $0 sets the name of the shell, which is used in warning and error messages.”

So your command just runs ls, while (uselessly) setting $0 to /Volumes. You want to run

/bin/bash -c ‘ls /Volumes’

which is what your final version does.

command executed via `ssh machine bash -c "..."` does not have access to /Volumes
 
 
Q