70

I can print my current working dir like this

myPrompt$ pwd
/Users/me/myDir

I want my shell to look like this

/Users/me/myDir$ pwd
/Users/me/myDir

Is that possible? How can I do it?

terdon
  • 242,166
bernie2436
  • 6,655
  • On OS X the name of the file is .bash_profile not just .profile. That will autoload for you. –  Aug 21 '14 at 21:54
  • 2
    Not quite. OSX starts login shells by default and that means that bash looks for ~/.bash_profile, ~/.bash_login, and ~/.profile, in that order, and reads and executes commands from the first one that exists and is readable. In any case, this is really a comment and not an answer so I am converting it to one. – terdon Aug 21 '14 at 22:03
  • If you came here looking for a way to do the opposite (i.e. stop showing working directory) you'll want PS1='\$ ' – tir38 Feb 08 '22 at 15:42

7 Answers7

83

You can use escape sequences in prompt variables.

Put this in your ~/.bashrc:

PS1='\w\$ '
8

Here's a one-liner for OSX. It appends the prompt you want into the profile file and then reloads the profile.

echo "PS1='\w\$ '" >> ~/.bash_profile; source ~/.bash_profile

On El Capitan you'll want to use

echo "PS1='\w\$ '" >> ~/.profile; source ~/.profile
Kit
  • 389
3

For some reason '\w\$' didn't work for me but instead I did: export PS1="$(pwd) \$" and it worked.

Stephen Kitt
  • 434,908
David
  • 39
  • 8
    That won’t work too well when you change directories... – Stephen Kitt Mar 15 '18 at 15:06
  • @StephenKitt, did you bother to try it? I tried and it works with directory change. Can't believe you have 8 upvotes for the comment. – PAS Jan 12 '23 at 17:53
  • @PAS yes, I did. The prompt gets set to whatever the current directory is when the export command is run, and doesn’t change when the current directory changes. – Stephen Kitt Jan 12 '23 at 18:46
  • I am not seeing these results. I can see directory changing. here is my test can't put new lines here...

    `/c/Program Files$ cd /c/temp/

    /c/temp$ cd /c/Program\ Files

    /c/Program Files$ cd /c/temp/

    /c/temp$ cd /c/Program\ Files Program Files/ Program Files (x86)/

    /c/temp$ cd /c/Program\ Files\ (x86)/

    /c/Program Files (x86)$`

    – PAS Jan 13 '23 at 19:27
  • @StephenKitt can you double check if your synatx is correct PS1="$PS1"'$(pwd)' # current working directory – PAS Jan 13 '23 at 19:32
  • @PAS that’s not what the answer says. I was commenting on the answer, not some putative corrected variant. – Stephen Kitt Jan 13 '23 at 21:37
  • @StephenKitt, you said "That won’t work too well when you change directories". This is not true on my machine using git bash. – PAS Jan 15 '23 at 03:07
  • @PAS using export PS1="$(pwd) \$", or PS1="$PS1"'$(pwd)'? – Stephen Kitt Jan 15 '23 at 08:23
  • I am not using export. It's directly in C:\Users\mylogin.config\git\git-prompt.sh do you have any profile file where you can put this instead of export? I do agree, exporting is a bad idea for this but the answer is valuable as $(pwd) is not mentioned anywhere. – PAS Jan 17 '23 at 19:49
1

Looks like an old thread but the steps below worked for me on OS X 10.9.5

  • put PS1='\w\$ ' in ~/.profile
  • if you made any changes in ~/.bashrc remove them
  • close the terminal with cmd+q
  • reopen the terminal
victor
  • 11
  • 2
  • 1
    In my case the complete PATH is really big so I have added one more flag for next line like PS1='\w\n$ ' – victor Oct 04 '15 at 17:58
1

For Python and Conda Users:

Add the following line to ~/.bashrc and run source ~/.bashrc after saving it. The following will show conda environment, username, device name, and base of current working directory. Additionally, it is colored cyan.

PS1="\e[0;36m ($CONDA_DEFAULT_ENV) \u@\h \W → \e[m "
1

My Suggestion

Put this in your ~/.bashrc:

PS1='${debian_chroot:+($debian_chroot)}\[\033[01;32m\]\u@\h\[\033[00m\](\D{%H:%M:%S})\[\033[01;34m\]:$PWD\[\033[00m\]\$ '

This will color line, and will also add time on every line (Time can be useful some time)

despotbg
  • 111
0

Simple solution

Open ~/.bash_profile and add the following content

# \[\e[0m\] resets the color to default color
reset_color='\[\e[0m\]'
#  \[\033[33m\] sets the color to yellow
path_color='\[\033[33m\]'
# \e[0;32m\ sets the color to green
git_clean_color='\[\e[0;32m\]'
# \e[0;31m\ sets the color to red
git_dirty_color='\[\e[0;31m\]'

# determines if the git branch you are on is clean or dirty
git_prompt ()
{
  # Is this a git directory?
  if ! git rev-parse --git-dir > /dev/null 2>&1; then
    return 0
  fi
  # Grab working branch name
  git_branch=$(git branch 2>/dev/null| sed -n '/^\*/s/^\* //p')
  # Clean or dirty branch
  if git diff --quiet 2>/dev/null >&2; then
    git_color="${git_clean_color}"
  else
    git_color="${git_dirty_color}"
  fi
  echo " [$git_color$git_branch${reset_color}]"
}

export PS1="${path_color}\w\[\e[0m\]$(git_prompt)\n"

This should:

1) Prompt the path you're in, in color: path_color.
2) Tell you which branch are you.
3) Color the name of the branch based on the status of the branch with git_clean_color 
for a clean work directory and git_dirty_color for a dirty one.
4) The brackets should stay in the default color you established in your computer.
5) Puts the prompt in the next line for readability.

You can customize the colors with this list

Sophisticated Solution

Another option is to use Git Bash Prompt, install with this. I used the option via Homebrew on Mac OS X.

git_prompt_list_themes to see the themes but I didn't like any of them.

git_prompt_color_samples to see available colors.

git_prompt_make_custom_theme [<Name of base theme>] to create a new custom theme, this should create a .git-prompt-colors.sh file.

subl ~/.git-prompt-colors.sh to open git-prompt-colors.sh and customize:

The .git-prompt-colors.sh file should look like this with my customization

    override_git_prompt_colors() {
      GIT_PROMPT_THEME_NAME="Custom"

      # Clean or dirty branch
      if git diff --quiet 2>/dev/null >&2; then
        GIT_PROMPT_BRANCH="${Green}"
      else
        GIT_PROMPT_BRANCH="${Red}"
      fi
    }

    reload_git_prompt_colors "Custom"

This answer is based on my SO answer which you can see here. Hope this helps, have a great day!

  • I think the OP was intending to simply display the shell's working directory. As a regular Git user I'm also using some fancy Git-aware prompt but that seems to be overkill here. – Martin Konrad Jun 08 '20 at 22:30