0

This issue is specific to MAC OS.

This is the code in the bash profile. Just updating the current directory path in the prompt

function __get_current_dir() {
    pwd
}
export PS1="\u@\h:--$(__get_current_dir)--\W$ "

Below is the behavior observed.

dummy@mac:--/Users/dummy--~$ pwd
/Users/dummy
dummy@mac:--/Users/dummy--~$ cd Desktop/
dummy@mac:--/Users/dummy--Desktop$ pwd
/Users/dummy/Desktop
dummy@mac:--/Users/dummy--Desktop$ cd ../Documents/
dummy@mac:--/Users/dummy--Documents$ pwd
/Users/dummy/Documents
dummy@mac:--/Users/dummy--Documents$ 

The output seen while executing pwd in the terminal shows proper path, but the same pwd evaluated in __get_current_dir is stuck with the path loaded when the terminal was opened the first time. The same code works properly in the ubuntu system.

Is there any fix for this ? The function __get_current_dir has a bit more code in it and it's logic is specific to the current directory. Since the pwd is not working here, the result of that function is wrong.

NEB
  • 712
  • 9
  • 25

1 Answers1

3

__get_current_dir is being evaluated when you define PS1. That means it's static. If you run echo "$PS1", you'll see \u@\h:--/Users/dummy--\W$.

To fix it, either use single-quotes, or escape the dollar sign with a backslash:

PS1='\u@\h:--$(__get_current_dir)--\W$ '
PS1="\u@\h:--\$(__get_current_dir)--\W$ "

BTW, there's no reason to export PS1.

Also BTW this is not specific to Mac OS, or even to variables.

wjandrea
  • 28,235
  • 9
  • 60
  • 81
  • Got it. Just read through the difference between single and double quote in bash, [string interpolation](https://stackoverflow.com/questions/6697753/difference-between-single-and-double-quotes-in-bash). – NEB May 11 '19 at 17:52