From Linux Bash Shell Scripting Tutorial Wiki
Jump to navigation Jump to search

The PS1 is a bash environment variable that one can set to modify the behavior of the shell and programs run from the shell.

Using PS1 shell variables

The Bash shell allows you to set shell variables that you can use to help run commands or modify the shell's behavior.

Assigning values to variables

The syntax is:


Retrieving values with variable expansion

Use the echo command or printf command to print the out the value of the variable:

echo "$VAR"
echo "$PS1"

If you enter the command echo VAR, it will print out the string VAR. Hence, always precede the name of the variable with a dollar sign ($). For example:

echo "$VAR" # correct
echo "PS1" # wrong (missing $)

Configuring Bash with shell variables

The PS1 is a shell variable that controls the appearance of the shell prompt. Changing the PS1 value will change the appearance of your shell prompt on Linux and Unix-like machines.

Change the your PS1 shell variable

Edit the ~/.bashrc config file:

nano ~/.bashrc


vim ~/.bashrc

Append or edit PS1 as follows:

# Simple example for PS1
PS1='[\u@\h \t \w]$ '
# Complicated but colorful (uncomment the following line)
# PS1="\[\e[31m\][\[\e[m\]\[\e[38;5;172m\]\u\[\e[m\]@\[\e[38;5;153m\]\h\[\e[m\] \[\e[38;5;214m\]\W\[\e[m\]\[\e[31m\]]\[\e[m\]\\$ "
export PS1

Save and close the file. Then log out and log in back to see your new PS1 settings.

See also