The course is part of this learning path
As the title suggests, this course looks at intermediate-level skills for those who already know a bit about Linux but want to enhance that knowledge. In this course, we build upon some of the topics covered in our Linux Fundamentals course, including files and shell scripting.
- Learn what wildcards are and how and when to use them
- Understand input, output, and redirection
- Work with files and shell scripting
- Anyone with basic knowledge of Linux who wants to learn more
- Professionals who want to learn more about Linux to enhance their career prospects
This is an intermediate-level course so some knowledge of Linux is expected. If you're just starting out, then try our Linux Fundamentals course first.
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.
Jason is the founder of the Linux Training Academy as well as the author of "Linux for Beginners" and "Command Line Kung Fu." He has over 20 years of professional Linux experience, having worked for industry leaders such as Hewlett-Packard, Xerox, UPS, FireEye, and Amazon.com. Nothing gives him more satisfaction than knowing he has helped thousands of IT professionals level up their careers through his many books and courses.