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: Alex fxmbsw7 Ratchev
Subject: Re: how to (safely) escape an arbitrary string for use in PS1
Date: Wed, 19 May 2021 21:47:40 +0200

another try..

my==$'special\ndata'
PS1='$my'

On Wed, May 19, 2021, 21:13 Alex fxmbsw7 Ratchev <fxmbsw7@gmail.com> wrote:

> maybe you wanted \$var and \$(cmd)
>
> On Wed, May 19, 2021, 21:11 Christoph Anton Mitterer <
> calestyo@scientia.net> wrote:
>
>> 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.
>>
>>


reply via email to

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