How can I see the verbatim contents of a shell variable?

Shuzheng

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?

Stephen Kitt

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.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How can I see contents of gzip file with paramiko?

From Dev

How can I print a summary of Maildir contents when logging into a shell?

From Dev

If a directory is a file, can I see the contents of that file?

From Dev

How can I easily see the man page for builtin shell commands?

From Dev

How can I edit a variable in a running shell?

From Dev

How can I edit a variable in a running shell?

From Dev

How can I emphasize or verbatim quote a comma in org mode?

From Dev

How can I check the verbatim characters of a bash command string?

From Dev

How can I treat strings read from SQL as verbatim?

From Dev

How I can escape a double quote in a verbatim string that contains variables

From Dev

How do I see the true contents of this string?

From Dev

How can I see the value of a variable without hovering over it in VBA?

From Dev

How can I efficiently see the contents of a renamed file maintained under git?

From Dev

How can I efficiently see the contents of a renamed file maintained under git?

From Dev

In Tcl, how do I prevent a variable's contents from being interpreted by the shell when passed to `exec`?

From Dev

How can I make echo or printf in shell ignore a double backslash (\\) and print it as I see it

From Dev

Where can I see the Contents of _root.xxx

From Dev

How can I check if a variable is empty or not in tcsh Shell?

From Dev

How can I pass a shell script variable into an Expect script?

From Dev

How can I assign the output of a command to a shell variable?

From Dev

How can I use $variable in a shell brace expansion of a sequence?

From Dev

How can I assign the output of this command to a variable in Shell?

From Dev

how can i declare variable in shell and use with condition instruction

From Dev

how can i use variable in jq?(MacOS,shell)

From Dev

Shell - how i can use a variable in sed command

From Dev

Using the MongoDB shell, how can I view a truncated version of the contents of a collection?

From Dev

How can I see more battery info when using Gnome shell?

From Dev

How can I see more battery info when using Gnome shell?

From Dev

How can I see if my password variable is within a line so that my code can proceed?

Related Related

  1. 1

    How can I see contents of gzip file with paramiko?

  2. 2

    How can I print a summary of Maildir contents when logging into a shell?

  3. 3

    If a directory is a file, can I see the contents of that file?

  4. 4

    How can I easily see the man page for builtin shell commands?

  5. 5

    How can I edit a variable in a running shell?

  6. 6

    How can I edit a variable in a running shell?

  7. 7

    How can I emphasize or verbatim quote a comma in org mode?

  8. 8

    How can I check the verbatim characters of a bash command string?

  9. 9

    How can I treat strings read from SQL as verbatim?

  10. 10

    How I can escape a double quote in a verbatim string that contains variables

  11. 11

    How do I see the true contents of this string?

  12. 12

    How can I see the value of a variable without hovering over it in VBA?

  13. 13

    How can I efficiently see the contents of a renamed file maintained under git?

  14. 14

    How can I efficiently see the contents of a renamed file maintained under git?

  15. 15

    In Tcl, how do I prevent a variable's contents from being interpreted by the shell when passed to `exec`?

  16. 16

    How can I make echo or printf in shell ignore a double backslash (\\) and print it as I see it

  17. 17

    Where can I see the Contents of _root.xxx

  18. 18

    How can I check if a variable is empty or not in tcsh Shell?

  19. 19

    How can I pass a shell script variable into an Expect script?

  20. 20

    How can I assign the output of a command to a shell variable?

  21. 21

    How can I use $variable in a shell brace expansion of a sequence?

  22. 22

    How can I assign the output of this command to a variable in Shell?

  23. 23

    how can i declare variable in shell and use with condition instruction

  24. 24

    how can i use variable in jq?(MacOS,shell)

  25. 25

    Shell - how i can use a variable in sed command

  26. 26

    Using the MongoDB shell, how can I view a truncated version of the contents of a collection?

  27. 27

    How can I see more battery info when using Gnome shell?

  28. 28

    How can I see more battery info when using Gnome shell?

  29. 29

    How can I see if my password variable is within a line so that my code can proceed?

HotTag

Archive