0

When I set my PS1='$PWD' the command line shows me the path to the current directory: /home/myname and it changes when I change the directory.

But when I change it to "$PWD" ( double quotes ) it always shows me the /home/myname no matter where I am at the moment. From what I've read it says that single quotes prints exactly what it is in it and don't expand special symbols like $. So why is it working that way?

talles
  • 14,356
  • 8
  • 45
  • 58
CuriousGuy
  • 1,545
  • 3
  • 20
  • 42

2 Answers2

1

The "$PWD" resolves immediately. So you're essentially setting PS1 to a fixed value (the value of PWD at the time it's set). When you set to '$PWD', it does not resolve immediately, so it resolves when used, and changes when you change directories. Thus the double quotes are expanding (to a fixed string) as expected, while single quotes are not.

user1676075
  • 3,056
  • 1
  • 19
  • 26
  • yes, but why tutorials say that single quotes print exactly whats in them? So '$PWD' should print "$PWD"? – CuriousGuy Jun 03 '13 at 18:02
  • Single quotes prevents resolution with respect to the current shell/command, while double quotes allow resolution in the current shell/command. The current shell/command is "set the value of the PS1 variable", not subsequent shell operations that read the value. Using double quotes is like literally writing "set PS1=/a/b/c" which becomes fixed. Using single quotes says "don't resolve the value during the set command", which results in it being resolved later, when using to display your prompt, so it changes. – user1676075 Jun 05 '13 at 12:39
0

PS1 is a special variable. From the ksh man page:

PS1    The  value  of  this variable is expanded for parameter expansion,
       command substitution, and arithmetic substitution  to  define  the
       primary  prompt string which by default is ``$''.  [...]

So, the value of PS1 gets special treatment before the prompt is displayed. When using single quotes, the value of PS1 is merely the string $PWD but when a prompt is required, ksh will further expands variables so the prompt gets your current directory.

glenn jackman
  • 238,783
  • 38
  • 220
  • 352