What is the correct location for PS1 Shell variable?

The PS1 shell variable should be set in ~/.bashrc for the bash shell as that is the initialisation file that is read for interactive shell sessions.

Note that this variable is a shell variable, not an environment variable (it does not make sense to let child processes inherit its value, and it's only the current shell that uses it). It therefore does not need to be exported with export.

Related:

  • What is the purpose of .bashrc and how does it work?

You shouldn't need to start bash from any of the shell's startup files. Starting a particular shell from ~/.profile (or the corresponding file related to your login shell) may possibly be warranted if the system that you're running on does not allow you to change your login shell. Care should be taken to not start the other shell if that is the shell already executing the file though, or you may end up in an infinite loop of sorts.

The exec code that you add to your ~/.bash_profile should never be needed. I suppose it's a way of getting ~/.bashrc to be parsed (it starts an interactive shell, and interactive bash shells read ~/.bashrc). A better way of doing that would be to have one of the files source the other, for example using this in ~/.bash_profile:

if [[ -f $HOME/.bashrc ]]; then
    source "$HOME/.bashrc"
fi

Then set PS1 in ~/.bashrc (there should be no need to touch HOME or TERM).

The other thing that the command does is to clean out all other environment variables using env -i. Unless you have very specific reasons to do this, you should not do that from your ordinary shell startup files.


To quote from the bash man-page:

When  bash is invoked as an interactive login shell, or as a non-interactive
shell with the --login option, it first reads and executes commands from the
file  /etc/profile,  if that file exists.  After reading that file, it looks
for ~/.bash_profile, ~/.bash_login, and ~/.profile, in that order, and reads
and  executes  commands from the first one that exists and is readable.  The
--noprofile option may be used when the shell is  started  to  inhibit  this
behavior.

When  a  login  shell  exits, bash reads and executes commands from the file
~/.bash_logout, if it exists.

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.  This may be inhibited by using the --norc option.  The --rcfile file
option  will  force  bash  to read and execute commands from file instead of
/etc/bash.bashrc and ~/.bashrc.

So it very much depends on how you start the shell.

  • If you want to have your PS1 active in all login shells (e.g. via su - <user> or when logging in remote via ssh), put it in profile.
  • If you want to have your PS1 active in all non-login shells (e.g. simply opening another terminal in your desktop environment), put in in bashrc.
  • If you want to have it active in both cases, you will need to put it in both files, or (which some Linux flavours do at least in the case of the system-wide /etc/profile and /etc/bash.bashrc), source the .bashrc in the .profile.

PS1 should be in .bashrc. You could also set it in .profile.

Debian sources .bashrc from there:

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