How do I get the current directory into my prompt?

How do I get the current directory into my prompt?


Bash provides a number of backslash-escape sequences which are expanded
when the prompt string (PS1 or PS2) is displayed. The full list is in
the manual page.

The \w expansion gives the full pathname of the current directory, with
a tilde (`~') substituted for the current value of $HOME. The \W
expansion gives the basename of the current directory. To put the full
pathname of the current directory into the path without any tilde
subsitution, use $PWD. Here are some examples:

PS1='\w$ ' # current directory with tilde
PS1='\W$ ' # basename of current directory
PS1='$PWD$ ' # full pathname of current directory

The single quotes are important in the final example to prevent $PWD from
being expanded when the assignment to PS1 is performed.



Home FAQ