Sometimes the simplest things 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 (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.

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


#!/bin/bash
#
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.