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:
Wed, 19 May 2021 21:10:58 +0200
User-agent:
Evolution 3.38.3-1
Hey.
Thanks, neither of them seems to work as desired:
The basic premise is e.g.:
foo='$(ls) \() ()'
and the content of $foo shall be used *literally* within PS1.
When doing:
$ PS1='$foo '
$(ls) \() ()
here it works, the prompt becomes that ugly string.
When doing:
$ PS1="$foo "
...
it doesn't work, $(ls) undergoes command substitution.
On Wed, 2021-05-19 at 20:21 +0200, Alex fxmbsw7 Ratchev wrote:
> you want the backslashes or so be visible ?
> try ${var@Q}
This seems to merely quote the string like the following:
printf '%s' "${foo@Q}"
'$(ls) \() ()'
But using that inside PS1:
PS1="${foo@Q} " # double quotes obviously
...
again leads to substitution
On Wed, 2021-05-19 at 20:22 +0200, Alex fxmbsw7 Ratchev wrote:
> alao aee printf %q directive
As I mentioned before, that doesn't work either:
$ printf '%q' "$foo"
\$\(ls\)\ \\\(\)\ \(\)
e.g. using:
escaped="$(printf '%q' "$foo")"
PS1="${escaped} "
$\(ls\)\ \(\)\ \(\)
so while the $ is correctly escaped, all kinds of other stuff, like the
parentheses, is too.
Cheers,
Chris.