help-bash
[Top][All Lists]
Advanced

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

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


From: Christoph Anton Mitterer
Subject: how to (safely) escape an arbitrary string for use in PS1
Date: Wed, 19 May 2021 19:30:03 +0200
User-agent: Evolution 3.38.3-1

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]