{"id":277,"date":"2009-12-04T21:24:28","date_gmt":"2009-12-04T21:24:28","guid":{"rendered":"http:\/\/kedar.nitty-witty.com\/?p=277"},"modified":"2023-05-14T10:52:25","modified_gmt":"2023-05-14T10:52:25","slug":"how-to-echo-colored-text-in-shell-script","status":"publish","type":"post","link":"https:\/\/kedar.nitty-witty.com\/blog\/how-to-echo-colored-text-in-shell-script","title":{"rendered":"How to echo colored text in linux shell script"},"content":{"rendered":"\n<p>Having a colourful display on shell script is something that&#8217;d beautify your experience. Using colour text and echos will help you to highlight and distinguish the shell output on a linux prompt.<\/p>\n\n\n\n<p>Here&#8217;s how you can have colored text in linux shell, try following command on your bash:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>bash $] tput setaf 1<br>You will see your text color will turn red.<br>To reset it you can type following command to make text color white<br><span style=\"color: #888888;\"> bash $] tput setaf 7<\/span><\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>or to reset everything again text modes<br><span style=\"color: #888888;\"> bash $] tput sgr0<\/span><\/code><\/pre>\n\n\n\n<p>Same things you can use in your bash script to colorize output.<\/p>\n\n\n\n<p>Create following sample bash script for colourize echo:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><em># vi try.sh <\/em>\n#<em>ColourfulText<\/em>\n<em>txtrst=$(tput sgr0) # Text reset<\/em>\n<em>txtred=$(tput setaf 1) # Red<\/em>\n<em>echo \"Welcome to ${txtred} kedar.nitty-witty.com ${txtrst}!\"<\/em><\/code><\/pre>\n\n\n\n<p><span style=\"color: #888888;\"><em><span style=\"color: #000000;\"><span style=\"font-style: normal;\">The coloured text output is as follows::<\/span><\/span><\/em><\/span><\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter\"><a href=\"http:\/\/kedar.nitty-witty.com\/wp-content\/uploads\/2009\/12\/colored-shell-script.JPG\"><img decoding=\"async\" src=\"http:\/\/kedar.nitty-witty.com\/wp-content\/uploads\/2009\/12\/colored-shell-script-300x60.jpg\" alt=\"colored-shell-script\" class=\"wp-image-278\" title=\"colored-shell-script\"\/><\/a><\/figure>\n<\/div>\n\n\n<p>If you need more colors, you may define them as follows in shell script:<\/p>\n\n\n\n<p>Other variables you can define as follows:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>txtgrn=$(tput setaf 2) # Green\ntxtylw=$(tput setaf 3) # Yellow\ntxtblu=$(tput setaf 4) # Blue\ntxtpur=$(tput setaf 5) # Purple\ntxtcyn=$(tput setaf 6) # Cyan\ntxtwht=$(tput setaf 7) # White\ntxtrst=$(tput sgr0) # Text reset.<\/code><\/pre>\n\n\n\n<p><strong>Following are the tput details further:<\/strong><br>tput setab [1-7] : Set a background colour using ANSI escape<br>tput setb [1-7] : Set a background colour<br>tput setaf [1-7] : Set a foreground colour using ANSI escape<br>tput setf [1-7] : Set a foreground colour<\/p>\n\n\n\n<p><strong>tput Text Mode Capabilities:<\/strong><\/p>\n\n\n\n<p>tput bold : Set bold mode<br>tput dim : turn on half-bright mode<br>tput smul : begin underline mode<br>tput rmul : exit underline mode<br>tput rev : Turn on reverse mode<br>tput smso : Enter standout mode (bold on rxvt)<br>tput rmso : Exit standout mode<br>tput sgr0 : Turn off all attributes (doesn&#8217;t work quite as expected)<br><br>I hope you liked this. Happy &amp; colourful shell scripting.&nbsp; \ud83d\ude42<\/p>\n","protected":false},"excerpt":{"rendered":"Having a colourful display on shell script is something that&#8217;d beautify your experience. Using colour text and echos will help you to highlight and distinguish the shell output on a&hellip;\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"om_disable_all_campaigns":false,"_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"footnotes":""},"categories":[6],"tags":[24,253,330,252,59],"class_list":{"0":"post-277","1":"post","2":"type-post","3":"status-publish","4":"format-standard","6":"category-technical","7":"tag-code","8":"tag-color-text-shell-script","9":"tag-colouring-shell-script","10":"tag-echo-coloured-text","11":"tag-linux"},"aioseo_notices":[],"amp_enabled":true,"_links":{"self":[{"href":"https:\/\/kedar.nitty-witty.com\/blog\/wp-json\/wp\/v2\/posts\/277","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/kedar.nitty-witty.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/kedar.nitty-witty.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/kedar.nitty-witty.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/kedar.nitty-witty.com\/blog\/wp-json\/wp\/v2\/comments?post=277"}],"version-history":[{"count":9,"href":"https:\/\/kedar.nitty-witty.com\/blog\/wp-json\/wp\/v2\/posts\/277\/revisions"}],"predecessor-version":[{"id":2897,"href":"https:\/\/kedar.nitty-witty.com\/blog\/wp-json\/wp\/v2\/posts\/277\/revisions\/2897"}],"wp:attachment":[{"href":"https:\/\/kedar.nitty-witty.com\/blog\/wp-json\/wp\/v2\/media?parent=277"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/kedar.nitty-witty.com\/blog\/wp-json\/wp\/v2\/categories?post=277"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/kedar.nitty-witty.com\/blog\/wp-json\/wp\/v2\/tags?post=277"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}