MacOS – Behavior of login shell vs. Linux, ‘sh -c’

bashcommand linemacos

I have a seemingly-valid command that works fine on Linux boxes but fails on OSX, and I would like to know why.

sudo su - validusername sh -c 'whoami'

On Linux, this will echo 'validusername' on the console. OSX however, shows an error:

/bin/sh: /bin/sh: cannot execute binary file

Is this an OSX particularity? Or is it due to the fact that OSX uses bash 3.2, while our linux boxes are on bash 4.x?

EDIT: I figured out a workaround quickly (sudo -u), I am mainly interested in learning why this happened. Thanks!

Best Answer

try

sudo -su validusername sh -c 'whoami'