1. Why can the command run by sudo inherit the environment from the parent shell process, while the shell process after sudo su to the root user doesn't?

    t@ocean:/tmp$ export ME=tim
    t@ocean:/tmp$ sudo echo $ME
    t@ocean:/tmp$ sudo su
    root@ocean:/tmp# echo $ME
    root@ocean:/tmp# exit
  2. Is the environment inherited from parent to child processes?

  3. Is the shell process after sudo su to the root user a child process of the previous user's shell process?


  1. The variable expansion is performed by your interactive shell. You're running the command sudo with the arguments echo and tim. If you want the expansion to happen in the shell invoked by sudo, tell it to run a shell and pass the string echo $ME to that shell:

    sudo sh -c 'echo $ME'

    sudo removes most variables from the environment, because they can be a security risk in some circumstances. See the manual and your machine's configuration for details. With typical configurations, the variable ME would be removed from the environment, so sudo sh -c 'echo $ME' would print nothing.

  2. The environment of a process is a copy of its parent, unless the parent decided to change it. (More precisely, the environment is passed to execve; most programs just pass through whatever they had.) Sudo is one of the programs that decides to change the environment.

  3. If you run sudo su, that runs a shell (because you didn't pass a command to su). The parent of that shell process is the process that invoked sudo, i.e. the shell where you ran the command sudo su.


The environment variable expansion is done by the shell so the command you're actually running is "sudo echo tim". This is all done before sudo is run.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.