MacOS – how to change the command prompt to show current working directory

command linemacosterminal

I'm trying to change my command prompt to display my current working directory, but it doesn't work for me. I have tried putting this in my bash_profile which is in my home directory:

PS1='\h:\w$ '

PS1='\h:$PWD \u$ '

and my prompt does change but it just shows the same as I'm putting in so it doesn't recognize things like \h and \w i think?

I'm running macOS Catalina 10.15.6enter image description here

Best Answer

You are using zsh so the definition needs to go to .zshrc and it's slightly different (see man zshmisc for all options). The equivalent for \h \w \$ in zsh is

PS1='%m %~%# '
  • %m The hostname up to the first '.'. An integer may follow the % to specify how many components of the hostname are desired. With a negative integer, trailing components of the hostname are shown.
  • %~ Current working directory. If an integer follows the %, it specifies a number of trailing components of the current working directory to show; zero means the whole path. If the current working directory starts with $HOME, that part is replaced by a ~
  • %# A # if the shell is running with privileges, a % if not.