{"id":13173,"date":"2019-04-01T04:58:16","date_gmt":"2019-04-01T04:58:16","guid":{"rendered":"http:\/\/www.appservgrid.com\/paw92\/?p=13173"},"modified":"2019-04-01T04:58:16","modified_gmt":"2019-04-01T04:58:16","slug":"how-to-customize-bash-colors-and-content-in-linux-terminal-prompt","status":"publish","type":"post","link":"https:\/\/www.appservgrid.com\/paw92\/index.php\/2019\/04\/01\/how-to-customize-bash-colors-and-content-in-linux-terminal-prompt\/","title":{"rendered":"How to Customize Bash Colors and Content in Linux Terminal Prompt"},"content":{"rendered":"<p>Today, Bash is the default shell in most (if not all) modern Linux distributions. However, you may have noticed that the text color in the terminal and the prompt content can be different from one distro to another.<\/p>\n<p><b>Suggested Read:<\/b>\u00a0<a href=\"https:\/\/www.tecmint.com\/different-types-of-linux-shells\/\" target=\"_blank\" rel=\"noopener\">5 Most Frequently Used Open Source Shells for Linux<\/a><\/p>\n<p>In case you have been wondering how to customize this for better accessibility or mere whim, keep reading \u2013 in this article we will explain how to do just that.<\/p>\n<h3>The PS1 Bash Environment Variable<\/h3>\n<p>The command prompt and terminal appearance are governed by an environment variable called\u00a0<code>PS1<\/code>. According to the\u00a0<strong>Bash<\/strong>\u00a0man page,\u00a0<strong>PS1<\/strong>\u00a0represents the primary prompt string which is displayed when the shell is ready to read a command.<\/p>\n<p>The allowed content in\u00a0<strong>PS1<\/strong>\u00a0consists of several backslash-escaped special characters whose meaning is listed in the\u00a0<strong>PROMPTING<\/strong>\u00a0section of the man page.<\/p>\n<p>To illustrate, let\u2019s display the current content of\u00a0<code>PS1<\/code>\u00a0in our system (this may be somewhat different in your case):<\/p>\n<pre>$ echo $PS1\r\n\r\n<strong>[\\u@\\h \\W]$<\/strong>\r\n<\/pre>\n<p>We will now explain how to customize\u00a0<strong>PS1<\/strong>\u00a0as per our needs.<\/p>\n<h4>Customizing the PS1 Format<\/h4>\n<p>According to the PROMPTING section in the man page, this is the meaning of each special character:<\/p>\n<ol>\n<li><code>\\u:<\/code>\u00a0the\u00a0<strong>username<\/strong>\u00a0of the current user.<\/li>\n<li><code>\\h:<\/code>\u00a0the\u00a0<strong>hostname<\/strong>\u00a0up to the first dot\u00a0<strong>(.)<\/strong>\u00a0in the Fully-Qualified Domain Name.<\/li>\n<li><code>\\W:<\/code>\u00a0the\u00a0<strong>basename<\/strong>\u00a0of the current working directory, with\u00a0<strong>$HOME<\/strong>\u00a0abbreviated with a tilde\u00a0<strong>(~)<\/strong>.<\/li>\n<li><code>\\$:<\/code>\u00a0If the current user is root, display\u00a0<strong>#<\/strong>,\u00a0<strong>$<\/strong>\u00a0otherwise.<\/li>\n<\/ol>\n<p>For example, we may want to consider adding\u00a0<code>\\!<\/code>\u00a0If we want to display the history number of the current command, or\u00a0<code>\\H<\/code>\u00a0if we want to display the FQDN instead of the short server name.<\/p>\n<p>In the following example we will import both into our current environment by executing this command:<\/p>\n<pre>PS1=\"[\\u@\\H \\W \\!]$\"\r\n<\/pre>\n<p>When you press\u00a0<strong>Enter<\/strong>\u00a0you will see that the prompt content changes as shown below. Compare the prompt before and after executing the above command:<\/p>\n<div id=\"attachment_24043\" class=\"wp-caption aligncenter\">\n<p><a href=\"https:\/\/www.tecmint.com\/wp-content\/uploads\/2017\/01\/Customize-Linux-Terminal-Prompt.png\"><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-24043\" src=\"https:\/\/www.tecmint.com\/wp-content\/uploads\/2017\/01\/Customize-Linux-Terminal-Prompt.png\" alt=\"Customize Linux Terminal Prompt PS1\" width=\"383\" height=\"83\" aria-describedby=\"caption-attachment-24043\" data-lazy-loaded=\"true\" \/><\/a><\/p>\n<p id=\"caption-attachment-24043\" class=\"wp-caption-text\">Customize Linux Terminal Prompt PS1<\/p>\n<\/div>\n<p>Now let\u2019s go one step further and change the color of the\u00a0<strong>user<\/strong>\u00a0and\u00a0<strong>hostname<\/strong>\u00a0in command prompt \u2013 both the text and its surrounding background.<\/p>\n<p>Actually, we can customize 3 aspects of the prompt:<\/p>\n<table border=\"0\" cellspacing=\"0\">\n<tbody>\n<tr>\n<td align=\"CENTER\" height=\"19\"><b>Text Format<\/b><\/td>\n<td align=\"CENTER\"><b>Foreground (text) color<\/b><\/td>\n<td align=\"CENTER\"><b>Background color<\/b><\/td>\n<\/tr>\n<tr class=\"alt\">\n<td align=\"CENTER\" height=\"16\">0: normal text<\/td>\n<td align=\"CENTER\">30: Black<\/td>\n<td align=\"CENTER\">40: Black<\/td>\n<\/tr>\n<tr class=\"alt\">\n<td align=\"CENTER\" height=\"16\">1: bold<\/td>\n<td align=\"CENTER\">31: Red<\/td>\n<td align=\"CENTER\">41: Red<\/td>\n<\/tr>\n<tr class=\"alt\">\n<td align=\"CENTER\" height=\"16\">4: Underlined text<\/td>\n<td align=\"CENTER\">32: Green<\/td>\n<td align=\"CENTER\">42: Green<\/td>\n<\/tr>\n<tr class=\"alt\">\n<td align=\"CENTER\" height=\"16\"><\/td>\n<td align=\"CENTER\">33: Yellow<\/td>\n<td align=\"CENTER\">43: Yellow<\/td>\n<\/tr>\n<tr class=\"alt\">\n<td align=\"CENTER\" height=\"16\"><\/td>\n<td align=\"CENTER\">34: Blue<\/td>\n<td align=\"CENTER\">44: Blue<\/td>\n<\/tr>\n<tr class=\"alt\">\n<td align=\"CENTER\" height=\"16\"><\/td>\n<td align=\"CENTER\">35: Purple<\/td>\n<td align=\"CENTER\">45: Purple<\/td>\n<\/tr>\n<tr class=\"alt\">\n<td align=\"CENTER\" height=\"16\"><\/td>\n<td align=\"CENTER\">36: Cyan<\/td>\n<td align=\"CENTER\">46: Cyan<\/td>\n<\/tr>\n<tr class=\"alt\">\n<td align=\"CENTER\" height=\"16\"><\/td>\n<td align=\"CENTER\">37: White<\/td>\n<td align=\"CENTER\">47: White<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>We will use the\u00a0<code>\\e<\/code>\u00a0special character at the beginning and an\u00a0<code>m<\/code>\u00a0at the end to indicate that what follows is a color sequence.<\/p>\n<p>In this sequence the three values (<strong>background<\/strong>,\u00a0<strong>format<\/strong>, and\u00a0<strong>foreground<\/strong>) are separated by commas (if no value is given the default is assumed).<\/p>\n<p><b>Suggested Read:<\/b>\u00a0<a href=\"https:\/\/www.tecmint.com\/category\/bash-shell\/\" target=\"_blank\" rel=\"noopener\">Learn Bash Shell Scripting in Linux<\/a><\/p>\n<p>Also, since the value ranges are different, it does not matter which one (<strong>background<\/strong>,\u00a0<strong>format<\/strong>, or\u00a0<strong>foreground<\/strong>) you specify first.<\/p>\n<p>For example, the following\u00a0<code>PS1<\/code>\u00a0will cause the prompt to appear in\u00a0<strong>yellow<\/strong>\u00a0underlined text with\u00a0<strong>red<\/strong>\u00a0background:<\/p>\n<pre>PS1=<strong>\"\\e[41;4;33m[\\u@\\h \\W]$ \"<\/strong>\r\n<\/pre>\n<div id=\"attachment_24044\" class=\"wp-caption aligncenter\">\n<p><a href=\"https:\/\/www.tecmint.com\/wp-content\/uploads\/2017\/01\/Change-Linux-Terminal-Color-Prompt.png\"><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-24044\" src=\"https:\/\/www.tecmint.com\/wp-content\/uploads\/2017\/01\/Change-Linux-Terminal-Color-Prompt.png\" alt=\"Change Linux Terminal Color Prompt PS1\" width=\"487\" height=\"117\" aria-describedby=\"caption-attachment-24044\" data-lazy-loaded=\"true\" \/><\/a><\/p>\n<p id=\"caption-attachment-24044\" class=\"wp-caption-text\">Change Linux Terminal Color Prompt PS1<\/p>\n<\/div>\n<p>As good as it looks, this customization will only last for the current user session. If you close your terminal or exit the session, the changes will be lost.<\/p>\n<p>In order to make these changes permanent, you will have to add the following line to\u00a0<code>~\/.bashrc<\/code>\u00a0or\u00a0<code>~\/.bash_profile<\/code>\u00a0depending on your distribution:<\/p>\n<pre>PS1=<strong>\"\\e[41;4;33m[\\u@\\h \\W]$ \"<\/strong>\r\n<\/pre>\n<p>Feel free to play around with the colors to find what works best for you.<\/p>\n<h5>Summary<\/h5>\n<p>In this article we have explained how to customize the color and content of your Bash prompt. If you have questions or suggestions about this post, feel free to use the comment form below to reach us. We look forward to hearing from you!<\/p>\n<p><a href=\"https:\/\/www.tecmint.com\/customize-bash-colors-terminal-prompt-linux\/\" target=\"_blank\" rel=\"noopener\">Source<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Today, Bash is the default shell in most (if not all) modern Linux distributions. However, you may have noticed that the text color in the terminal and the prompt content can be different from one distro to another. Suggested Read:\u00a05 Most Frequently Used Open Source Shells for Linux In case you have been wondering how &hellip; <\/p>\n<p class=\"link-more\"><a href=\"https:\/\/www.appservgrid.com\/paw92\/index.php\/2019\/04\/01\/how-to-customize-bash-colors-and-content-in-linux-terminal-prompt\/\" class=\"more-link\">Continue reading<span class=\"screen-reader-text\"> &#8220;How to Customize Bash Colors and Content in Linux Terminal Prompt&#8221;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-13173","post","type-post","status-publish","format-standard","hentry","category-linux"],"_links":{"self":[{"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/posts\/13173","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/comments?post=13173"}],"version-history":[{"count":1,"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/posts\/13173\/revisions"}],"predecessor-version":[{"id":13174,"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/posts\/13173\/revisions\/13174"}],"wp:attachment":[{"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/media?parent=13173"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/categories?post=13173"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/tags?post=13173"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}