Set bash variable every time directory change

I would like the variable to have my bash shell available, similar to pwd , but equal to the section of the current working directory, and not the whole path.

i.e.,

 $PWD=/a/b/c/d/e/f $PATH_SECT=c/d/e 

I have a prompt that already displays this path, but I would like to update the variable in the environment to this value every time I change the directory.

How can i do this?

+6
source share
2 answers

You can use the promptcmd function. From man bash we learn that this function is executed just before the prompt is displayed. It is empty by default (or rather undefined).

A simple example:

 promptcmd(){ local p=$(pwd) PATH_SECT=${p/\/a\/b\/} } 
+5
source

You can use alias and function in .bashrc :

 alias cd="supercd" # call the function function supercd(){ builtin cd " $@ " # original cd PATH_SECT=$(pwd) # or whatever } 
+1
source

Source: https://habr.com/ru/post/924045/


All Articles