Georgi Georgiev
Georgi Georgiev

Reputation: 1623

Single/double quotes ksh

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?

Upvotes: 0

Views: 814

Answers (2)

glenn jackman
glenn jackman

Reputation: 246847

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.

Upvotes: 0

user1676075
user1676075

Reputation: 3086

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.

Upvotes: 1

Related Questions