Why The. Bashrc File Is Not Invoked After a Sudo Command?

I switch to another user using the command:

sudo -u dsc -i

After the command executions moves to the home directory /home/dsc .

However, the .bashrc file in this directory is not executed. Why this happens?

2

2 Answers

sudo -i starts a login shell; .bashrc is only executed for non-login interactive shells. One of .bash_profile, .bash_login, or .profile should have been executed, though.

2

I noticed that su -l as well as sudo -i was indeed not loading the .bashrc file.
However, after I added a .profile file (in this case at /root/.profile) that loads the .bashrc file when present, everything works fine

# if running bash
if [ -n "$BASH_VERSION" ]; then
    # include .bashrc if it exists
    if [ -f "$HOME/.bashrc" ]; then
        . "$HOME/.bashrc"
    fi
fi

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Chloe Bennett

Chloe Bennett

Culture, Media & Entertainment Columnist

Chloe Bennett explores the intersection of pop culture, streaming entertainment, digital trends, and contemporary lifestyle. Her weekly commentary reaches thousands of culture enthusiasts.

Share this article
Twitter Facebook Pinterest