Setting the PS1 Environment Variable In Redhat Linux Distributions

There have been instances where administrators have had difficulty setting the PS1 environment variable on their system when using Redhat Linux distributions. The PS1 environment variable controls the prompt on the command line, and can be used by users to tell what system they are on, the directory they are currently in, the current date and more depending on how this variable is configured. This tip will explain the strange method used by Redhat distributions to control the PS1 variable, and the options administrators have to work around it.

Understanding the Problem

When Bash begins to run when the user logs in, the following sequence of events will normally occur unless Bash is invoked with the -noprofile option. These events are specific for a common Redhat distribution upon initial install. Please see the How Linux Works CTDP Guide for complete information on files that are run when bash starts, or read the bash(1) man page.

  1. Bash runs /etc/profile if it exists
  2. Bash runs $HOME/.bash_profile
    1. The $HOME/.bash_profile script runs the $HOME/.bashrc script
      1. The $HOME/.bashrc script runs /etc/bashrc
Note: The $HOME name used above indicates the user home directory. If you examine the sequence of events above, it is obvious that the last step is rather unusual. Normally scripts are run for system wide control, then scripts that are individually set up for specific users are run last. In the above sequence of events a script set for system control is run after running two scripts that are in the user's home directory. This has the effect of stepping on any values any individual user may set the PS1 environment variable to. When a user makes a modification to this variable in the .bash_profile script in their home directory, the change will be ineffective. This is because the /etc/bashrc file contains the following:
# /etc/bashrc

# System wide functions and aliases
# Environment stuff goes in /etc/profile

# For some unknown reason bash refuses to inherit
# PS1 in some circumstances that I can't figure out.
# Putting PS1 here ensures that it gets loaded every time.
PS1="[\u@\h \W]\\$ "

This will set the PS1 variable to the value shown here. The PS1 value is normally initially set in the /etc/profile script for system wide default use, then the individual users may modify or change this value in the $HOME/.bash_profile script for their own use. If you note the comment above, the writer of the /etc/bashrc file states that "bash refuses to inherit PS1 in some circumstances".

Solving the Problem

Normally, the correct thing to do would be to run the /etc/bashrc script from the /etc/profile script. The /etc/bashrc script should not change the PS1 variable, but is normally used to set up aliases, Therefore in addition to doing the below changes the administrator may want to comment out the line in /etc/bashrc that sets the PS1 variable, and add the three lines from the $HOME/.bashrc file that run the /etc/bashrc script to the end of the /etc/profile script. The /etc/bashrc script can then be used by the administrator for setting global alias values.

I think setting the PS1 variable in the $HOME/.bash_profile or $HOME/.bashrc script should be sufficient to avoid the above problem so long as you be sure to set it. Since the $HOME/.bashrc file contains the following:

# .bashrc

# User specific aliases and functions

# Source global definitions
if [ -f /etc/bashrc ]; then
	. /etc/bashrc

The easiest thing to do is comment out the three lines that make up the if statement (put the # character at the start of each line), which would run the /etc/bashrc script and add the following line or one like it to the file to set the PS1 value.

PS1="[\u@\h \w]\\$ "

This line can be added to the $HOME/.bash_profile script file instead but would involve changing two files. Adding it to the $HOME/.bash_profile script is the more appropriate thing to do from a system standpoint since the $HOME/.bashrc file is for aliases. The administrator would want to make these changes to all these files in their user's home directories, or have their users do it.

Making the change for all future users

The directory /etc/skel contains files that are used by the system to create files for new users in their home directories. To make this change effective for users that are added to the system in the future, the /etc/skel/.bashrc file should be changed and the /etc/skel/.bash_profile file should be changed if it was used to set the PS1 variable.

The meaning of the characters in the prompt string settings

The following list shows the meanings of the special characters used to define the PS1 and PS2 prompt strings.

If you want the full path of the current directory, use a small w in the string shown above. Read the bash(1) man page for more information. Also read The Bash Reference Manual in the directory /usr/doc/bash2-doc-2.03/ It can be accessed from an X session by double clicking on it while using the file manager. Bash builtins are described in the file in the directory /usr/doc/bash2-doc-2.03/

Comments or Problems

If anyone finds that they have some difficulties in making this change that may be related to this tip, please send an email to the administrator of this website describing the distribution of Linux being used, the version of bash and any other circumstances that pertain to the problem. Also include copies of $HOME/.bash_profile, and $HOME/.bashrc. We cannot guarantee a response, but will do what we can to look into the problem and update the tip as it is appropriate.

Date of Original Tip:
July 12, 2000
Mark Allen
Linux Tips Contents Page