Search code examples
linuxbashshellterminalsudo

What happens when I sudo bash -c?


I know that sudo bash -c 'some_command' will run some_command with the same privileges as sudo.

I'm confused as to what's happening? Does it run some_command in bash as sudo (same as sudo bash) then switch back to my current user? Why am I not left in an instance of bash with sudo privileges like I would when I run sudo bash?

I tried running man bash and it describes the -c option (quoted below). However, I am struggling to piece how the description relates to the behaviour I observed when running sudo bash -c 'some_command'

If the -c option is present, then commands are read from the first non-option argument command_string. If there are arguments after the command_string, they are assigned to the positional parameters, starting with $0.


Solution

  • sudo switches users and then executes bash, passing it the other arguments. bash running as the new user runs the command in the argument after -c.