I want to view the verbatim contents of a shell variable on Debian containing terminal escape codes. If I try to echo
its contents, then all of the terminal escape gets interpreted by the terminal to e.g. display colours.
➜ sds git:(master) echo $PS1
%(?:%{%}➜ :%{%}➜ ) %{$fg[cyan]%}%c%{$reset_color%} $(git_prompt_info)
Is it possible to print the verbatim contents of a shell variable, or do I need to find the exact script that sets it in order to view its verbatim contents?
As terdon says, you can’t get back the original construction of the variable if it was constructed using the values of other variables.
However you can see its contents, uninterpreted by the terminal or even by the shell itself, in at least Bash and Zsh:
printf '%q\n' "$PS1"
This will escape all the characters which would otherwise have a different effect; you can use this to reconstruct the verbatim contents.
Another approach is to output the variable’s contents to somewhere other than the terminal (directly); for example
printf %s "$PS1" | od -vtc
will show the variable’s contents, character by character, replacing control characters with a mnemonic (nl
, esc
, sp
...).
Do not use echo
here which expands \x
sequences and adds an extra newline character.
In your case,
%(?:%{%}➜ :%{%}➜ ) %{$fg[cyan]%}%c%{$reset_color%} $(git_prompt_info)
looks like it could be exactly the verbatim contents of your PS1
variable: there are no terminal-interpreted escapes there, those are indirectly obtained through the fg
array and reset_color
variable. Assuming the promptsubst
option is set, those would be expanded when PS1
is used to display the prompt, but not when echo $PS1
is expanded.
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments