4 ms·
On the site all prompt variables end in "tput sgr0". I haven't seen that before. What does that do and why is it needed?
by the__prestige 5y ago
On the site all prompt variables end in "tput sgr0". I haven't seen that before. What does that do and why is it needed?
- whelming_wave 5y ago`tput sgr0` reads terminfo to find out how to issue a command which resets all currently set colors, text properties, and so on, then resets them. A lot of people will do something like `echo -e '\E[32;46m'`, but that's arguably more opaque and definitely not portable across terminals. tput is a neat command that I wish got used more! I also use `tput cnorm` to ensure the cursor is visible.
- sirn 5y agotput query a terminfo database and return an escape code suitable for that $TERM. sgr0 is the name for "reset" (e.g. "\033[0;10m" in ANSI). Using tput instead of escape code directly is better in a lot of ways. For example, it allows the shell script to be adaptive to the type of TERM it runs on (e.g. not outputting color codes at all if TERM doesn't support colors). You can see what each tput commands are doing by running infocmp. Most TERM implement ANSI-compatible escape codes, but may prefer other escape codes for the same effect (e.g. try comparing `infocmp -1 ansi` and `infocmp -1 xterm` and `infocmp -1 screen`). You can see the entire list of these commands in terminfo(5) (`man 5 terminfo`) in Predefined Capabilities section.
- dolmen 5y agotput uses the terminfo database which is usually located in /usr/share/terminfo. If your terminal ($TERM) is not known to the operating system, you can also import the terminfo definition from another OS in a directory and point the TERMINFO environment variable to it (useful if the legacy OS you connect to via SSH doesn't have full support for your fancy terminal emulator).
- mixmastamyk 5y agoOthers explained why. But, it’s not really been necessary since maybe the early 90s, unless you have hardware terminals around? It’s more efficient to echo CSI 0m, in shell than to run another process to do the same. Every mainstream terminal supports it.
- mmphosis 5y agoRather than turn off all attributes with "tput sgr0", instead I Save Cursor with "tput sc" at the beginning to save attributes, and Restore Cursor with "tput rc" at the end to restore attributes. Save and Restore cursor doesn't always work because I don't necessarily need the vertical position restored, for example: ls_color () { tput sc; /bin/ls --color=auto "$@"; push_cursor_position; tput rc; pop_cursor_position }