ARCHIVED: In Unix, how do I make my current directory appear in the prompt?

This content has been archived, and is no longer maintained by Indiana University. Information here may no longer be accurate, and links may no longer be available or reliable.

The way to make the prompt in Unix display your current directory will depend on which shell you are using. It's easy with some shells, but hard or impossible with others. Follow the instructions below for the shell you're using.

C shell (csh)

Put the following in your .cshrc file, and customize the prompt variable as you wish:

  alias setprompt 'set prompt="${cwd}% "'
  setprompt             # to set the initial prompt
  alias cd 'chdir \!* && setprompt'

If you use pushd and popd, you'll also need:

  alias pushd 'pushd \!* && setprompt'
  alias popd  'popd  \!* && setprompt'

Note: Some C shells don't keep a $cwd variable, but you can use pwd instead.

If you just want the last component of the current directory in your prompt (for example, mail% instead of /usr/spool/mail%), you can use:

  alias setprompt 'set prompt="$cwd:t% "'

Bourne shell (sh)

If you have a newer version of the Bourne shell (SVR2 or newer), you can use a shell function to make your own command, for example:

  xcd() { cd $* ; PS1="`pwd` $ "; }

Korn shell (ksh)

Put the following line in your .profile file:

  PS1='$PWD $ '

If you just want the last component of the directory to appear in your prompt, use:

  PS1='${PWD##*/} $ '

TC shell (tcsh)

The tcsh shell is a popular enhanced version of csh with some extra built-in variables:

%~
The current directory, using ~ for $HOME
%/
The full pathname of the current directory
%c or %.
The trailing component of the current directory

This allows you to use the following command:

  set prompt='%~ '

bash (FSF's Bourne-again shell)

A \w in $PS1 gives the full pathname of the current directory, with ~ expansion for $HOME. A \W gives the basename of the current directory. Thus, in addition to the above sh and ksh solutions, you could use either of the following:

  PS1='\w $ '   
  PS1='\W $ '

Note:
This information comes from the Unix FAQ.

Related documents

This is document abee in the Knowledge Base.
Last modified on 2018-01-18 09:31:52.