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 20:22:43 +0200

alao aee printf %q directive

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

> you want the backslashes or so be visible ?
> try ${var@Q}
>
> good luck ans best wishes
>
> On Wed, May 19, 2021, 20:03 Christoph Anton Mitterer <
> calestyo@scientia.net> wrote:
>
>> Hey there.
>>
>> With PS1 one cannot now for sure whether the promptvars shell option is
>> on or not (and it can be changed at any time).
>>
>> So when one wants to add an arbitrary string to PS1 (i.e. one that
>> could also contain shell meta characters), the only things one can
>> basically do is:
>>
>> a) add it wrapped within a variable that is then expanded, e.g.:
>> string="$(< /some/file)"
>> PS1='${string}...'
>>
>> which has however the the drawbacks of:
>> - $string must remain for the lifetime of the shell
>> - if promptvars is disabled, it looks ugly
>> - every time the prompt is printed, all stuff needs to be expanded
>>
>>
>> b) escape the string and included it directly, e.g.:
>> escapedString=...
>> PS1="${escapedString}..."
>>
>> Obviously, if one wouldn't escape the string here, but use it directly,
>> a string like:
>> string='$(rm -rf /)'
>> would be pretty bad.
>>
>>
>> Now is there any safe way for escaping that?
>> I guess bash's builtin printf '%q' cannot be used here, because it
>> escapes more than what PS1 would expand/substitute.
>>
>>
>> Thanks,
>> Chris.
>>
>>
>>


reply via email to

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