Debian – Change PS1 value for all Bash users

bashbashrcdebian

I am trying to change the prompt value for all users of a system (the $PS1 variable) to the same value.

I have the following stored in file /etc/ps1:

PS1='`
  if [ $? -eq 0 ];
    then echo -n "\[\033[00;35m\]\u\[\033[01;32m\]@\[\033[00;35m\]\h\[\033[00;32m\](\[\033[01;35m\]\W\[\033[01;32m\])\[\033[00;32m\]\$";
    else echo -n "\[\033[00;35m\]\u\[\033[01;31m\]@\[\033[00;35m\]\h\[\033[01;31m\](\[\033[35m\]\W\[\033[31m\])\[\033[00;31m\]\$";
  fi`\[\033[0m\]'

Under my single user account I can add source /etc/ps1 to my ~/.profile file it works (interestingly it didn't work when I added that to ~/.bashrc). If I add this to /etc/profile or /etc/bashrc.basrch to make this happen for all users, nothing happens. I'm puling my hair out trying to get this to work. This is on Debian 7.1.0 (Linux 3.2.46).

Best Answer

Add your modified PS1 setting to /etc/profile.d/custom_ps1.sh. Files under /etc/profile.d are automatically sourced from /etc/profile:

if [ -d /etc/profile.d ]; then
  for i in /etc/profile.d/*.sh; do
    if [ -r $i ]; then
      . $i
    fi
  done
  unset i
fi

Which is called whenever a login shell is spawned. From the bash manpage:

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.