问题描述
当我设置PS1 ='$ PWD'时,命令行会向我显示当前目录的路径:/home/myname
,当我更改目录时它会更改.
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.
但是当我将其更改为"$ PWD"(双引号)时,无论我现在在哪里,它总是向我显示/home/myname.从我读过的书中可以看出,单引号可以准确打印出其中的内容,并且不会扩展$之类的特殊符号.那么为什么这样工作呢?
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?
推荐答案
"$ PWD"立即解析.因此,您实际上是将PS1设置为固定值(设置时PWD的值).当您设置为'$ PWD'时,它不会立即解析,因此在使用时会解析,并且在更改目录时也会改变.因此,双引号会按预期扩展(到固定的字符串),而单引号则不会.
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.
这篇关于单引号/双引号ksh的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!