ENV - PS1

maelamrani

Member
Joined
Jan 3, 2020
Messages
77
Reaction score
7
Credits
450
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
 


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

No members online now.

Latest posts

Top