[Date Prev][Date Next] [Thread Prev][Thread Next] [Date Index] [Thread Index]

Re: Bash and the PS1 environment variable [was: grep: show matching line from pattern file]



On Thu, Jun 02, 2022 at 06:01:11PM -0700, David Christensen wrote:
> This is my PS1.  '\u' does not work on all of Debian, FreeBSD, Cygwin, and
> macOS, so the expansion of ${USER} is inserted between two string literals
> when .profile runs and sets PS1:
> 
> 2022-06-02 17:39:09 dpchrist@laalaa ~
> $ grep PS1 .profile
> export PS1='\n\D{%Y-%m-%d %H:%M:%S} '${USER}'@\h \w\n\$ '

Variable expansions *are* performed when PS1 is evaluated.  So, you
could simply do:

PS1='stuff $USER more stuff'

That will delay the expansion of USER (which by the way is a BSD-ism)
until the prompt is drawn.  The way you've written it, $USER is
expanded at the time PS1 is assigned.  Which is not wrong, so long as
the value of USER cannot change in the middle of a shell session...
but it's not how most of the experienced people would do it, I think.

I'm rather curious how you managed to find a system + bash version
where \u doesn't work.  That sounds like something you'd want to report
to the bug-bash mailing list... or, possibly, a user error.  \u should
work on any Unix-like system.


Reply to: