102

I need to use a "clean" shell (e.g. bash) under Linux/OSX terminal without any user configuration, but it reads config info from some files (e.g ~/.bashrc) every time it starts. I can modify the file every time I need a "clean" shell, and revert it back when I finished, but is there any easier ways to do this, for example a command?

1
  • The answers provided below are also useful when, say, one makes a mistake in their .bashrc or .bash_profile files that causes the shell to exit prematurely. Apr 28, 2020 at 2:39

4 Answers 4

155

Running bash --noprofile --norc still inherited from parent process. Based on a similar question I found that the way I interpreted this question env -i bash --norc --noprofile was what I would want.

7
  • 9
    Thanks for the env -i tip! This ended up being a little too clean for me, since I just wanted to get rid of my custom settings, but I still needed the system-wide settings. This is what I ended up using: env -i bash --rcfile /etc/profile
    – DaoWen
    Jul 17, 2015 at 18:49
  • Great tip. The "env -i" is exactly what I needed. My bash_profile was still being read in despite the "--noprofile --norc" . Jan 24, 2017 at 19:55
  • 1
    You didn't need the argument --noprofile as it's only relevant if you have a login shell.
    – Chad
    May 21, 2017 at 8:07
  • 2
    It still inherits the history. Sep 23, 2019 at 9:52
  • 1
    @thc according to man page, -i, --ignore-environment start with an empty environment
    – David Xia
    Feb 23, 2022 at 13:33
67

You can pass the --noprofile and --norc command-line options:

$ bash --noprofile --norc

You will find documentation about these options in the man page.

4
  • 31
    bash --noprofile --norc still inherits from parent process.
    – nnutter
    Oct 18, 2015 at 18:24
  • 3
    This was good enough for me. Got rid of my aliases, prompts, etc.
    – wisbucky
    Sep 15, 2017 at 22:33
  • 1
    env -i PATH=/usr/bin:/bin "TERM=$TERM" "HOME=$HOME" bash --noprofile --norc
    – Harmen
    Jun 15, 2022 at 13:55
  • 1
    In order to also avoid any configuration files and dirs from the home directory (.condarc, .gitconfig, etc.) you also need a new temporary empty home directory, as per my answer below.
    – Pierre D
    Sep 22, 2022 at 16:42
9

Use --noprofile --norc:

   --noprofile
          Do  not  read either the system-wide startup file /etc/profile or any of the personal initializa‐
          tion files ~/.bash_profile, ~/.bash_login, or ~/.profile.  By default,  bash  reads  these  files
          when it is invoked as a login shell (see INVOCATION below).

   --norc Do  not  read  and  execute the system wide initialization file /etc/bash.bashrc and the personal
          initialization file ~/.bashrc if the shell is interactive.  This option is on by default  if  the
          shell is invoked as sh.

(from the manpage).

7

It is often desirable to launch an entirely blank bash:

  • no environment variables carried from the parent shell;
  • an empty home dir without any package-specific configuration files (e.g. .gitconfig and .local/...);
  • no shell configuration files.

This works for me both on MacOS and Linux:

env -i HOME=$(mktemp -d) bash --noprofile --norc
cd

In that bash shell, the HOME dir is that test dir just created (change the name if needed), and there are no particular settings. The only environment variables that are set are PWD, HOME, and SHLVL.

Upon starting bash, the PWD is where we were before, so we need to do that initial cd.

Example (Linux):

$ env -i HOME=$(mktemp -d) bash --noprofile --norc
bash-5.0$ cd
bash-5.0$ pwd
/tmp/tmp.mwgHRQE1aJ
bash-5.0$ printenv
PWD=/tmp/tmp.mwgHRQE1aJ
HOME=/tmp/tmp.mwgHRQE1aJ
SHLVL=1
OLDPWD=/home/xxxxxxxxxxxxxxxxx
_=/usr/bin/printenv
bash-5.0$ 

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.

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