To know, what is my CWD in console, I added the following to ~/.bash_profile:

export PS1='$(pwd)$ '

So, pwd result it shown before the $ symbol. It was very nice.

But when I go to a directory with a long name: /var/www/vhosts/beta.sitename.co.uk/httpdocs/sites/beta.sitename.co.uk/modules/bundles

In the command prompt I see:

.co.uk/modules/bundles$ sitename.co.uk/httpdocs/sites/beta.sitename.

This makes the shell almost impossible to work with. Is it possible to trim it, keeping only the last part or the dir name? Say, 30 characters.

Or, maybe the better idea would be to print the pwd after every cd command, so I could know, where I am? This can be done using an alias.


In my .bashrc, my PS1 is configured to display the last component of my current working directory. This can be done using \W (PS1 allows some special sequences along with variables). In your case, that would be:

$ export PS1='\W$'

More of these sequences may be found in man bash, at Prompting, see here. You may also use \w to use complete paths, but with $HOME abbreviated to ~ (which would shorten most of your paths). Now, if you really want to put a character limit, you may use...

$ export PS1='$(pwd | tail -c30)$' # Limited to 30 characters
$ export PS1='...$(pwd | tail -c30)$'

... or with a little bashism:

$ export PS1='${PWD: -30}$'
$ export PS1='${PWD:(-30)}$'
$ export PS1='...${PWD: -30}$'
$ export PS1='...${PWD:(-30)}$'
| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.