ENV - PS1

maelamrani

Member
Credits
323
Hello i have a question,
when i
export PS1="<$LOGNAME in $PWD>"
source .bashrc
/home/user
cd /etc
</home/user>
again even if i change the directory

but if i
export PS1="<$LOGNAME in \$PWD>"
source .bashrc
/home/user
cd /etc
</etc>
it update the PWD in the OUTPUT

Can someone explain me this plz
 


JasKinasis

Well-Known Member
Credits
5,160
In the first example - $PWD is being evaluated by the shell when the value of PS1 is being set. So it’s basically the same as setting it to always be "/home/username" (or whatever the current working directory is/was at the time that you set the PS1 variable.)
So, effectively it hard codes PS1 to use the initial working directory.

Escaping the $ using \ in the second example prevents the $PWD variable from being evaluated when PS1 is set.

Instead, $PWD is evaluated/de-referenced at runtime, whenever PS1 is used.
That way the current working directory will always appear in PS1.

It’s a very subtle difference. I’m finding it difficult to explain clearly, but hopefully that makes sense?!
 

Members online


Latest posts

Top