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 17:09:05 +0200
User-agent: Evolution 3.38.3-1

On Thu, 2021-05-20 at 09:40 +0900, Koichi Murase wrote:
> It seems that you want the way to escape a string such that the
> string
> would be printed literally for both sides of the option `promptvars'.
Exactly.


> I suspect it is impossible.
Hmm...

> The first one is the escape sequences for the prompts, such as \u for
> usernames, etc. To escape the string from the first phase, you can
> just replace all the backslashes with double backslashes.
Yes that would still be easy.


> The second one is the evaluation of ${...}, $((...)), $(...), `...`,
> \", \\, \$, \`, etc. The problem is that `shopt -u promptvars'
>  completely turns off the second phase. If you think about the case
> `shopt -u promptvars', you need to pass the literal string to this
> phase. But, if you also think about the case `shopt -s promptvars',
> that string should not be modified by the second phase. It is not
> possible for arbitrary strings.

I tried to first quote all \ with 's/\\/\\\\/g;'
Next all $ with something like this 's/\$/\\\$/g'

But the problem then always seems to be the special meaning of \$ in
PS1:
It doesn't seem to be possible to escape $.
- $ alone might get used with promptvars.
- \$ is $ or #
- \\$ gives \$


So the only safe way seems to be to always put arbitrary strings in a
tmp var.

Which of course has the unpleasant drawbacks of
a) needing such a var in the environment, which may be overrwritten by
anything execute in the shell environment.

b) looking ugly if somone switches off promptvars



Or does anyone else have an idea?


Thanks,
Chris.





reply via email to

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