Is it possible to have environment variables in the path of the working directory : PS1?

1

2

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.

mthpvg

Posted 2012-10-26T10:13:09.333

Reputation: 113

Answers

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

Mat

Posted 2012-10-26T10:13:09.333

Reputation: 6 193

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 – 2012-10-26T11:13:18.867

The demo of Mat completely answers my question. Thanks Mat. I was understanding the substitution with the link he gave. – mthpvg – 2012-10-26T11:27:04.100

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.

samir

Posted 2012-10-26T10:13:09.333

Reputation: 231

It was more like after changing of directory several times or copy paste from a different terminal. Thanks anyway. – mthpvg – 2012-10-26T11:15:45.683

1@mthpvg Are you aware of the pushd and popd tools? – Bernhard – 2012-10-28T09:30:35.730

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 – 2012-10-29T08:58:16.197

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

mthpvg

Posted 2012-10-26T10:13:09.333

Reputation: 113