How can I change my bash prompt to show my working directory?
166,066
Solution 1
You can use escape sequences in prompt variables.
Put this in your ~/.bashrc
:
PS1='\w\$ '
Solution 2
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
Solution 3
For some reason '\w\$'
didn't work for me but instead I did: export PS1="$(pwd) \$"
and it worked.
Solution 4
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
Related videos on Youtube
Author by
bernie2436
Updated on September 18, 2022Comments
-
bernie2436 over 1 year
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?
-
Admin over 9 yearsOn OS X the name of the file is .bash_profile not just .profile. That will autoload for you.
-
terdon over 9 yearsNot 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. -
tir38 over 2 yearsIf you came here looking for a way to do the opposite (i.e. stop showing working directory) you'll want
PS1='\$ '
-
-
bernie2436 over 10 yearsthanks. I made that change. Now I see: Abrams-MacBook-Air-3:tmp abramhandler$ pwd; /Users/abramhandler/tmp Abrams-MacBook-Air-3:tmp abramhandler$ .... I want to only show the part that says "tmp" for my prompt. How do I hide the rest? I want to hide the "abramhandler" and "Abram's-MacBook-Air-3" parts
-
terdon over 10 years@akh2103 use
\W
instead of\w
. See the link Gilles gave you for a (short) explanation of the escape codes. -
bernie2436 over 10 years@terdon I see the same thing with \W and \w. I am running Unix on OSX.
-
terdon over 10 years@akh2103 if you are running OSX and using the terminal app, you should make changes to ~/.profile not .bashrc since terminal runs a login shell by default. You should also source ~/.profile or open a new terminal for the changes to take effect. Also, please edit to add more information, I can't understand what you are asking for from your last comment.
-
bernie2436 over 10 years@terdon when I run source ~/.profile it changes the path properly in the current terminal. But when I open a new terminal then I still see "abram's mac book air" etc. I tried adding source ~/.profile to both the .profile and .bashrc files but still no luck. Any ideas?
-
terdon over 10 years@akh2103 on OSX? Weird, that sounds like
~/.profile
is not being read so I guess~/.bashrc
is instead. I don't use OSX but apparently, the default terminal app launches login shells and ignores~/.bashrc
. If you have a~.bash_profile
edit that instead. Feel free to @ ping me in chat if it still does not work. -
victor over 8 yearsIn my case the complete PATH is really big so I have added one more flag for next line like PS1='\w\n\$ '
-
Stephen Kitt about 6 yearsThat won’t work too well when you change directories...
-
Martin Konrad almost 4 yearsI 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.
-
Vivek over 2 years
echo "PS1='\u:\w\$ '" >> ~/.bash_profile; source ~/.bash_profile
to show prompt with username