Search code examples
shellubuntuscriptingsudo

sudo whoami vs sudo echo `whoami`


Simply put, why is this? Wouldn't it make more sense that sudo echo whoami returns root too?

Cheers

me:~$ whoami
me
me:~$ sudo whoami 
root
me:~$ sudo echo `whoami`
me

Solution

  • This is happening because whoami get substituted prior the sudo and echo. Basically:

    sudo echo `whoami`
    

    first becomes

    sudo echo me
    

    and then it sudo get executed.