1. Home
  2. Training Library
  3. Linux Administration Bootcamp Introduction | CSL4 V2 A3.1 |

Customizing the Shell Prompt

Contents

keyboard_tab
Linux Administration Bootcamp
Installing and Connecting to a Linux System
2
Linux Boot Process and System Logging
33
Disk Management in Linux
36
Partitions
PREVIEW6m 50s
38
User Management in Linux
Shell Scripting with Linux
54
55
Start course
Overview
Difficulty
Beginner
Duration
7h 28m
Students
2
Description

In this course, you will learn how to install a Linux system and connect to it, whether that be on Mac or Windows.

Transcript

This lesson will cover how to customize your shell prompt. As you've seen earlier in this course, default prompts can vary from system to system. No matter what shell you're using, you can customize your prompt by setting an environment variable. For shells like bash, ksh and sh, the environment variable PS1 is used to set the primary prompt string. For shells like csh, tcsh, and zsh, they use a prompt environment variable. The format string you place in the environment variable determines the look and feel of your prompt.

Each shell uses slightly different format string so consult the documentation for the shell that you're using. Let's look at customizing the bash prompt since bash is the most popular default shell for user accounts on Linux systems. These are some of the most commonly used formatting string options for bash. /d will give you the date in the week, day, month date format, /h will give you the host name up to the first dot or the short host name, a capital H will give you the fully qualified domain name of the server, /n will give you a new line so you can have a multiline prompt, /t will give you the current time in 24 hour format while /T will give you the current time in 12 hour format.

An n% will give you the current time in 12 hour format with am and pm included, /A will give you the current time in 24 hour format just using hours and minutes, /u is the username of the current user, /w is the current working directory, /W will give you the base name of the current working directory instead of the full path, /$ will let you know if you're a normal user, it will display a dollar sign or if you the super user or root account, it will display a pound sign. For a complete listing of all the formatting options, see the bash man page.

To make your customized shell prompt persist between logins, add the PS1 value to your personal initialization files. Personal initialization files are commonly referred to as dot files since they begin with a dot or a period. You could also use nano, vi or Emacs to edit your dash bash underscore profile file. This example just uses echo to accomplish the same thing.

Let's look at our current prompt string and let's change it in some way. Let's set it to username at host and you can see that it's customized now. Let's add the time to our prompt and the working directory too. And the tilde you can see represents our home directory. Let's add, let's go to the var log, so you can see that var log is the current directory and let's use base names for directories this time with a W. Now you can see that even though we're in /var/log, it only says log.

Let's go back to our home directory, if we want to persist changes between logins, we need to put these in our personalization file. So let's edit bash profile, set this environment variable. We'll just set it to this to show that it's going to work. We'll include our username, the hosts we are on, the directory we're in and a dollar. And now you can see that the prompt is retained between logins.

Let's do one more thing, let's make a multiline prompt. We'll put time on one line and let's put host and working directory on the next line. Clear the screen here and you can see that every time that we hit enter, we get a new prompt with the current time on one line and a normal looking prompt on the second line.

Shell prompts can be customized by setting an environment variable. This can differ from shell to shell. Lot of shells use PS1, others use prompt. To save your custom shell prompt between login, you need to add that environment variable setting to your personal initialization files such as dot bash profile.

About the Author