David J Murray Dev
~ Learn and Do - Excel and Share ~

Change the Linux command prompt interactively

Sep 18, 2016
You're Reading: Change the Linux command prompt interactively

A post in Code & Coding & FixUps & Linux

Sometimes the simplest things like changing the Linux command line prompt interactively almost seems impossible.

If you use long descriptive directory (folder) names and like to work at the terminal, it can quickly become a problem as you go deeper into the directory levels as the prompt displays the complete path from the home directory and becomes rather long.

The command line prompt is set by PS1 and by default is: (e.g. on Linux Mint 18 XFCE):

$ echo $PS1
\[\e]0;\u@\h \w\a\]${debian_chroot:+($debian_chroot)}\[\033[01;32m\]\u@\h\[\033[00m\] \[\033[01;34m\]\w \$\[\033[00m\]

The issue is that \w is used to display the complete path from home. A better option is to use \W as this will display the last directory name only.

These special characters are found with a ‘man bash’ and search for PROMPTING ‘/PROMPTING’.

Searching on-line provides many articles about how to change the PS1 prompting text, but they always change this is the ‘.bashrc’ file etc. I want to change this on the fly when I enter a directory and the path becomes too long.

By chance (persistence and hope) I stumbled across this article:  https://www.linux.com/learn/how-make-fancy-and-useful-bash-prompt-linux which uses the ‘source’ bash command.

Why the source command? Source is part of the bash shell software and more is found with “man bash” and a search for “source”. The issue is that when you type a command at the shell prompt, a new shell is created to run the command. Any changes to the shell environment are then lost when the command terminates. The “source” command will run within the current shell environment without starting a new one. Those of you who use the virtual python environments should find this familiar.

Changing the command line prompt.

So, create a script (say changePS1.sh), changing the ‘\w’ to ‘\W’ and copying the rest as is:

PS1="\[\e]0;\u@\h \w\a\]${debian_chroot:+($debian_chroot)}\[\033[01;32m\]\u@\h\[\033[00m\] \[\033[01;34m\]\W \$\[\033[00m\]"

And at the shell prompt, use the ‘source’ command:

$ source changePS1.sh

Problem solved – an interactive script to change the terminal prompt when I want to.

Topic in course: https://davidjmurray.dev/courses/the-mit-missing-semester-of-your-cs-education/lessons/course-overview-the-shell/topic/using-the-shell/


About the Author

<a href="https://davidjmurray.dev/losucag/davidjmurray/" target="_self">djm</a>


I have been in the computer/IT industry for over 35 years. After spending half a life-time in highly proprietary database environments, I have succumbed to the WordPress Eco-system and I'm well down the chasm. I am specializing in AWS hosting, Wordpress CMS, Elegant Themes, in particular Divi and Extra themes.


Submit a Comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Pin It on Pinterest

Share This