Super User is a question and answer site for computer enthusiasts and power users. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I am logged in as the root of a Linux 2.6.18 machine. I changed the value of PS1 as:

export PS1="$PWD> "

It works and hardcodes the command prompt as the home directory followed by a >. Now I want it to show the current directory that I am on now.

How to I set this? Is there a document that explains the codes that explain how the PS1 variable works?

share|improve this question
up vote 3 down vote accepted

Try this

export PS1="[\w]$ "

here

w represents current working directory

for more info see this link


Edit: These are the special characters that bash understands in PS1 and PS2

\a : an ASCII bell character (07)
\d : the date in "Weekday Month Date" format (e.g., "Tue May 26")
\D{format} : the format is passed to strftime(3) and the result is inserted into 
             the prompt string; an empty format results in a locale-specific time 
             representation. The braces are required
\e : an ASCII escape character (033)
\h : the hostname up to the first '.'
\H : the hostname
\j : the number of jobs currently managed by the shell
\l : the basename of the shell’s terminal device name
\n : newline
\r : carriage return
\s : the name of the shell, the basename of $0 (the portion following the final 
     slash)
\t : the current time in 24-hour HH:MM:SS format
\T : the current time in 12-hour HH:MM:SS format
\@ : the current time in 12-hour am/pm format
\A : the current time in 24-hour HH:MM format
\u : the username of the current user
\v : the version of bash (e.g., 2.00)
\V : the release of bash, version + patch level (e.g., 2.00.0)
\w : the current working directory, with $HOME abbreviated with a tilde
\W : the basename of the current working directory, with $HOME abbreviated with 
     a tilde
\! : the history number of this command
\# : the command number of this command
\$ : if the effective UID is 0, a #, otherwise a $
\nnn : the character corresponding to the octal number nnn
\\ : a backslash
\[ : begin a sequence of non-printing characters, which could be used to embed a 
     terminal control sequence into the prompt
\] : end a sequence of non-printing characters
share|improve this answer
    
That worked! Thanks! But why did $PWD not work here, but elsewhere? – VSN Jun 29 '12 at 7:05
    
@VSN: Because $PWD was expanded at the time you set PS1. Consider this: "export MYPROMPT=aaa; export PS1="$MYPROMPT". This would set your prompt to "aaa". If you then "export MYPROMPT=bbb" your prompt will not change because PS1 only stored the contents of MYPROMPT, not a reference to the variable MYPROMPT. – Terje Mikal Jun 29 '12 at 8:20
    
@Terje Mikal: I see. But how does PS1="$PWD> " work on other Linux machines, then? Going by this philosophy, it shouldn't work anywhere. Can you think of any reason? – VSN Jun 29 '12 at 8:26
    
@RedGrittyBrick: That's very useful information. Thanks a lot! Could you think of any reason why it doesn't work here but elsewhere it does? – VSN Jun 29 '12 at 14:17
    
@VSN On my Linux systems, PS1="$PWD> " does not update the prompt after a cd. Maybe on your other systems cd is an alias which updates PS1. – RedGrittyBrick Jun 29 '12 at 15:03

can you make sure you really run bash, and not some sort of busybox whatsoever?

echo $SHELL

should show you the shell. (I assume /bin/bash)

then you can look with

ls -l /bin/bash

if it points somewhere else (a link) or if it is the actual executable

share|improve this answer
    
I did check. I was running /bin/bash/ – VSN Jun 29 '12 at 8:28

This works on other linux machines. I don't know why it isn't working here.

Perhaps because on the other machines you have something like

$ PS1="\$(pwd)> "
/tmp> cd /
/>
share|improve this answer

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .