help-bash
[Top][All Lists]
Advanced

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

Re: how to (safely) escape an arbitrary string for use in PS1


From: Christoph Anton Mitterer
Subject: Re: how to (safely) escape an arbitrary string for use in PS1
Date: Thu, 20 May 2021 23:41:29 +0200
User-agent: Evolution 3.38.3-1

I couldn't stop looking into this, and something seems a bit strange
(or at least I don't understand it ^^):

In a live shell (i.e. the next line shows the prompt that has just been
set before):
starting with PS1='$ '
$ PS1='\ '   ===> clear, "\ " has no special meaning to bash
\ PS1='\\ '  ===> clear, too, \\ yields \
\ PS1='\\\ '
             ===> don't understand I'd have expected that e.g. '\\\ ' gives '\\ 
'
                  i.e. the first '\\' give one '\' and then a sole '\ ' remains
\ PS1='\\\\ '
\ PS1='\\\\\ ' => why does it take 5 \ to get double \\
\\ 



Similarly with '$' (as root here):
PS1='$ '
$ PS1='\$ '
# PS1='\\$ '
$ PS1='\\\$ '
\# PS1='\\\\$ '
\$ PS1='\\\\\$ '
\# PS1='\\\\\\$ '
\$ PS1='\\\\\\\$ '





btw:
The following lets bash go into an endless loop (Ctrl-C doesn't help):
export bar='\h $(echo \$(date))' ; qbar="$(printf '%s' "$bar" | sed 
's/\\/\\\\/g; s/\$/\\\\\\\\$/g')"; printf '%s\n%s\n' "$bar" "$qbar" ; 
PS1="$qbar \$ "

I'm not quite sure whether I understand why.


Cheers,
Chris.




reply via email to

[Prev in Thread] Current Thread [Next in Thread]