{
"$type": "site.standard.document",
"canonicalUrl": "https://rednafi.com/misc/terminal-text-formatting-with-tput/",
"description": "Format terminal output with tput instead of ANSI codes. Set colors, bold text, underlines, and backgrounds with simple commands in shell scripts.",
"path": "/misc/terminal-text-formatting-with-tput/",
"publishedAt": "2023-04-23T00:00:00.000Z",
"site": "at://did:plc:fgtm2c26vfcj74rfmeggbyqj/site.standard.publication/3mnl6f7ob462z",
"tags": [
"Shell",
"Unix",
"TIL",
"CLI"
],
"textContent": "When writing shell scripts, I'd often resort to using hardcoded [ANSI escape codes] to\nformat text, such as:\n\nThis shell snippet above shows how to add text formatting and color to shell script output\nvia ANSI escape codes. It defines a few variables that contain different escape codes for\nbold, unbold, foreground, and background colors. Then, we echo two log messages with\ndifferent colors and formatting options.\n\nThe first message is printed in bold red text on a yellow background, while the second\nmessage is printed in white text on a blue background. To ensure that subsequent output is\nnot affected by the previous formatting, the RESET variable is used to reset all color and\nformatting options back to their defaults after each message is printed. The -e option is\nused with echo to enable the interpretation of backslash escapes, which includes the ANSI\nescape codes.\n\nWhile this works fairly well, every time I have to write a fancy shell script, I have to\neither look up the ANSI color codes, copy-paste from an existing script, or explain to an\nLLM what I need. Then chatGPT serendipitously recommended a shell tool called tput that\nmakes this workflow quite a bit better. Underneath tput also uses ANSI escape codes to\ncontrol various text formatting options but it doesn't require you to hardcode these ugly\nescape codes.\n\nBasic usage\n\nThe basic syntax of the tput command goes as follows:\n\nFormatting options\n\nHere are some commonly used tput formatting options:\n\n- setaf <color>: set the foreground (text) color to a specific color. For example,\n setaf 1 sets the color to red, while setaf 2 sets the color to green.\n- setab <color>: set the background color to a specific color.\n- bold: set the text to bold.\n- sgr0: reset all formatting options to their defaults.\n- smul: underline the text.\n\nExample usage\n\nRunning the script will give you the following output:\n\n![Terminal output showing tput text formatting with colored warning and debug messages][image_1]\n\nThis also hardcodes the color and formatting codes but it's much easier than having to\nremember or search for the ANSI escape codes. Currently, I'm using a 256-bit macOS terminal\nand it supports fairly large sets of formatting options. You can run man tput to find out\nother features that are supported by your terminal. The following loop will print all the\nsupported colors:\n\nOn my terminal, it prints this nice color palette:\n\n![Terminal displaying 256-color palette grid generated with tput setab command][image_2]\n\n\n\n\n[ansi escape codes]:\n https://gist.github.com/fnky/458719343aabd01cfb17a3a4f7296797\n\n[image_1]:\n https://blob.rednafi.com/static/images/terminal_text_formatting_with_tput/img_1.png\n\n[image_2]:\n https://blob.rednafi.com/static/images/terminal_text_formatting_with_tput/img_2.png",
"title": "Simple terminal text formatting with tput"
}