In Ubuntu default .bashrc there's this:

# If not running interactively, don't do anything
[ -z "$PS1" ] && return

Does anyone know why that is there? Is there something in the bashrc that is bad to run if the shell is ran non-interactively?


The point of the .bashrc file is that it sets the shell up to be more convenient for interactive users. Helpful alias, pretty colors, useful prompts, common environment variables, that sort of thing.

And some of these conveniences could break non-interactive scripts. For example:

`alias rm='rm -i'`

Would not work non-interactively (thanks zwets).

  • What I don't understand: If bash is not started as an interactive shell, then .bashrc isn't read, anyway, is it? I put the line echo ".bashrc is being sourced" in my .bashrc before the [ -z "$PS1" ] && return and then ran bash -c "echo" and is does not show... – Stefan Hamcke Oct 26 '18 at 16:03
  • Okay, I could of course do bash -c "source ~/.bashrc", then it IS sourced :-) – Stefan Hamcke Oct 26 '18 at 16:10
  • From man bash: "When an interactive shell that is not a login shell is started, bash reads and executes commands from /etc/bash.bashrc and ~/.bashrc, if these files exist." So it should make one wonder why that line exists if an non-interactive shell doesn't read the file anyway. The answer: unix.stackexchange.com/q/257571 – Quasímodo Jan 23 at 20:37

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.