Ask Ubuntu is a question and answer site for Ubuntu users and developers. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I want something like this:

"vivek@grishma:~/xxx/yyy/zzz/src$" to be shown as

"vivek@grishma:datasource$" where I would somehow have predefined "datasource" to be an alias for the long path above.

using the alias command as

" alias datasource='~/xxx/yyy/zzz/src'"

is useful for navigation but it does not take out the long path in the prompt.

Is this possible?

PS- I dont want it to be just "vivek@grishma:" as then each time I should run pwd to know my working directory.

share|improve this question
also see… – Stormvirux Apr 1 '14 at 14:04
@guntbert The link you have suggested "trims" the path and does not "change" it to another name. So I guess this question is very different from the one you have linked – Vivek V K Apr 2 '14 at 9:45
up vote 6 down vote accepted

This will do:

    case $PWD in
       $HOME/xxx/yyy/zzz/src) echo "datasource ;; 
       *) echo "\w" ;; 

That gives you the flexibility to define other special directories as well.

Aliases won't help you here.

To reduce duplication, put all your special dirs in an array, and use that to generate your aliases and also your prompt. Put all this in your ~/.bashrc:

declare -A labels=(
for path in "${!labels[@]}"; do
    alias "${labels[$path]}"="$path"
function path_label () {
    local IFS=:
    if [[ ":${!labels[*]}:" == *:"$PWD":* ]]; then
        # we're in a "known" dir
        echo "${labels[$PWD]}"
        return 1
PS1='\u@\h:$( path_label || echo "\w" )\$'
share|improve this answer
Thanks a lot. does exactly what I want. Can this be mended to take the alias even if it is part of the path? What I mean is, if I have $HOME/foo/bar/bob can this become baz/bob ??? – Vivek V K Apr 1 '14 at 16:20
in bash, aliases can only be aliases for commands. They cannot be substituted just anywhere, they are only commands. But yes, you can modify that code to just substitute a prefix of $PWD – glenn jackman Apr 1 '14 at 20:16

Put the following scripts in your ~/.bashrc

if [ "$(pwd)" == "$HOME/xxx/yyy/zzz/src" ]; then
    PS1='\u@\h:datasource$ '

Go to the directory ~/xxx/yyy/zzz/src and to change the prompt

. ~/.bashrc

In other directory, to get back the original prompt again source your ~/.bashrc.

I don't think you need an alias for that. Always you can use an alias like

alias src='. ~/.bashrc'
share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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