Single/double quotes ksh

[亡魂溺海] 提交于 2019-12-11 19:55:15

问题


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?


回答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.




回答2:


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.



来源:https://stackoverflow.com/questions/16902814/single-double-quotes-ksh

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!