1

NOTE: Yes, This is on Mac OS 11.6.2 Big Sur.

Right now I have zsh aliases in the .zshrc file to make terminal navigation easier. I thought I'd try to colorize the PWD output, but what happens is that the output is the previous directory. I've added an additional PWD command to show the current directory, but the colorized one is always the last one:

alias goto-config="cd ~/checkouts/trunk/service/configs;pwd;echo -e '\n\e[1;32m $(pwd) \e[0m\n'"


actual input/output:

User.Me % goto--config
/Users/Me/checkouts/trunk/service (this line is standard text color) 

/Users/Me/directory/from/which/command/was/called (this line is in color)

expected input/output:

User.Me % goto--config
/Users/Me/checkouts/trunk/service (this line is standard text color) 

/Users/Me/checkouts/trunk/service (this line is in color)

So, I'm confused. I thought the echo of the $(pwd) would echo the same directory path as we just navigated to, like it's displayed above. What am I doing wrong?

Thanks!

3
  • By "expected input/output" you actually mean your actual output right? I cannot reproduce the behavior on my machine. In my case pwd correctly prints the directory I'm in after cd. Commented Mar 22, 2022 at 16:38
  • Yes, the actual output. I should note this is on Mac OS 11.6.2 Big Sur.
    – MaxRocket
    Commented Mar 22, 2022 at 20:02
  • You might want to try using && instead of ;. Unfortunately I don't have access to a system with MacOS to test different things. Commented Mar 23, 2022 at 5:33

1 Answer 1

0

While I don't have a real solution to the problem as I don't have access to a system with MacOS at the moment, I'll still write down an answer to give you different possible workarounds.


Store path in variable

alias asdf="dir=~/checkouts/trunk/service/configs ; cd $dir ; pwd ; echo -e '\n\e[1;32m $(echo $dir) \e[0m\n'"

In this example the path will be obtained from $dir, not from the output of pwd.


use /bin/pwd

It seems like MacOS contains different implementations of the pwd command. You can try to use /bin/pwd as described here.


use pwd -P

Usually by default pwd just prints the content of $PWD. You can tell it with the -P flag to print the physical location instead:

pwd: pwd [-LP]
Print the name of the current working directory.

Options:
  -L    print the value of $PWD if it names the current working
    directory
  -P    print the physical directory, without any symbolic links

By default, `pwd' behaves as if `-L' were specified.

use a zsh function

You could create a function inside your .zshrc file instead of an alias:

goto-config() 
{
    cd ~/checkouts/trunk/service/configs
    pwd
    echo -e "\n\e[1;32m $(pwd) \e[0m\n"
}

Please let me know if one of the methods did work.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .