1

I am on Lubuntu and I am using bash. My PS1 (in .bashrc) is :

PS1="\w> "

I like it because I need to paste the working directory all the time. The problem is that the path is always very long and since I use terminator I only have half of my screen's width available to display it... it is ugly and annoying. My command prompt looks like that :

/this/is/a/very/long/path/that/i/want/to/make/shorter >

I'd like to set in my environment variables :

$tiavl=/this/is/a/very/long

And then I'll get :

$tiavl/path/that/i/want/to/make/shorter >

The goal is to have something shorter in the command prompt but I still want to be able to copy paste it and do :

cd $tiavl/path/that/i/want/to/make/shorter

It is a bit like with $HOME :

~/path/that/i/want/to/make/shorter  >

I know where I am and I can copy paste the ~.

Thanks.

2

You can do this with a little helper function, as below (use /home as an example prefix path):

~ > pwd
/home/me
~ > tiavl=/home
~ > prompt_path () { echo ${1/#$tiavl/\$tiavl}; }
~ > export PS1="\$(prompt_path \w) > "
$tiavl/me > 

This uses a simple string manipulation function (see here for lots of examples) in the function to replace the initial part of the path with a literal $tiavl if it matches.

Here's a demo of how to update that function for several paths.

#! /bin/sh

path1=/home
path2=/usr
path3=/var

prompt_path() {
    local path
    path="${1/#$path1/\$path1}"
    path="${path/#$path2/\$path2}"
    path="${path/#$path3/\$path3}"
    echo "$path"
}

prompt_path $HOME
prompt_path /usr/local
prompt_path /var/tmp
  • Seems good to me. A question : if I want to have several prefix paths, I can not see how it can work. I write some bash scripts but I am not confident with string manipulation... I am reading your link. – mthpvg Oct 26 '12 at 11:13
  • The demo of Mat completely answers my question. Thanks Mat. I was understanding the substitution with the link he gave. – mthpvg Oct 26 '12 at 11:27
1

If you copy paste in the terminal, you can just use $PWD environment variable which will always show you the working directory.

echo $PWD

shows the working directory.

  • It was more like after changing of directory several times or copy paste from a different terminal. Thanks anyway. – mthpvg Oct 26 '12 at 11:15
  • 1
    @mthpvg Are you aware of the pushd and popd tools? – Bernhard Oct 28 '12 at 9:30
  • Thanks for that, always thought it was like "cd -"... it is not. But it is only working within one terminal. I am gonna use it anyway :). – mthpvg Oct 29 '12 at 8:58
0

So basically you make a bash script :

prompt_path

which contains :

#! /bin/sh
path="${1/#$path1/\$path1}"
path="${path/#$path2/\$path2}"
path="${path/#$path3/\$path3}"
echo "$path"

You place it in a folder like :

~/.local/bin

You give x right to your script :

chmod u+x prompt_path

In your ~/.bashrc :

1 - you change PS1 to :

PS1="\$(prompt_path \w) > "

2 - you add those lines :

export path1=/home
export path2=/usr
export path3=/var

3 - you indicate so that you can call your script from anywhere:

export PATH=~/.local/bin:$PATH

Finally you source your .bashrc :

. ~/.bashrc

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.