UNIX Tutorials, Tips, Tricks and Shell Scripts

How to Include the Current/Working Directory in your Command Prompt (the PS1 shell variable)


The tip "Changing your Command Prompt (the PS1 shell variable)" showed how to customize the command prompt by redefining the PS1 shell variable. The example used for this tip demonstrated how you could add your username and the hostname to the prompt.

Another desirable command prompt customization is to display the current directory (also known as the working directory) as part of the prompt. When the cd command is used to change to another directory, the prompt should automatically be updated to reflect the change.

This functionality can be achieved by defining PS1 as follows:

export PS1=[${LOGNAME}@$(hostname)]'$PWD>'

Similar to the first tip on the PS1 variable, the username and hostname have also been included in this example.

Taking this new definition for a test drive illustrates how it is updated each time the cd command is executed:

$ pwd
/export/home/livefire
$ export PS1=[${LOGNAME}@$(hostname)]'$PWD>'
[livefire@hawk]/export/home/livefire>cd /tmp
[livefire@hawk]/tmp>cd /usr/bin
[livefire@hawk]/usr/bin>cd ..
[livefire@hawk]/usr>cd
[livefire@hawk]/export/home/livefire>

Notice that using cd with no arguments is a quick way to return to your home directory. Over time you will discover how useful this feature is.

Do you need to learn UNIX or Linux, including how to read and write shell scripts? If you are ready to move past the basics, either of these online courses is a good place to start...

UNIX and Linux Operating System Fundamentals contains a very good "Introduction to UNIX Shell Scripting" module, and should be taken if you are new to the UNIX and Linux operating system environments or need a refresher on key concepts.

UNIX Shell Scripting is a good option if you are already comfortable with UNIX or Linux and just need to sharpen your knowledge about shell scripting and the UNIX shell in general.

Both courses include access to an Internet Lab system for completing the course's hands-on exercises, which are used to re-enforce the key concepts presented in the course. Any questions you may have while taking the course are answered by an experienced UNIX technologist.