Just to update slightly (for Python3) and enhance the selected answer to add colours to the prompt as per a BASH prompt (in Linux Mint 18.3 anyway):
#! /usr/bin/python3
import os, getpass
from socket import gethostname
username = getpass.getuser()
hostname = gethostname()
pwd = os.getcwd()
homedir = os.path.expanduser('~')
pwd = pwd.replace(homedir, '~', 1)
if len(pwd) > 40:
# first 10 chars+last 30 chars
pwd = pwd[:10] + '...' + pwd[-30:]
# Virtual environment being used? Essential not to omit!
ve = os.getenv('VIRTUAL_ENV')
venv = '(`basename \"$VIRTUAL_ENV\"`)' if ve else ''
# colours as per my current BASH Terminal:
# username + hostname: bold green
# path and $: bold blue
print( '\[\e[;1;32m\]%s%s@%s \[\e[;1;34m\]%s $\[\e[0m\] ' % (venv, username, hostname, pwd) )
More on colour codes in a BASH Terminal here. There's probably some way of finding out what colours your Terminal uses automatically, but I haven't got a clue what that might be.
With the shebang line the export
line for inclusion in .bashrc then becomes:
export PROMPT_COMMAND='PS1="$(~/.local/bin/manage_prompt.py)"' # adjust path to .py file
NB1 these "\e" escape codes must always be enclosed in "\[ ... \]", otherwise line-returns get completely messed up.
NB2 to get your full path at any time just go
... $ pwd
of course...