{"id":1437,"date":"2014-03-02T21:17:52","date_gmt":"2014-03-03T02:17:52","guid":{"rendered":"http:\/\/www.dannyadam.com\/blog\/?p=1437"},"modified":"2018-09-13T15:09:12","modified_gmt":"2018-09-13T20:09:12","slug":"color-coded-bash-prompt","status":"publish","type":"post","link":"https:\/\/www.dannyadam.com\/blog\/2014\/03\/color-coded-bash-prompt\/","title":{"rendered":"Color Coded Bash Prompt"},"content":{"rendered":"<p>This post explains what I&#8217;m referring to by\u00a0<em>color coded bash prompt<\/em>, why it is useful to have one, and presents an implementation.<\/p>\n<p>By <em>color coded bash prompt<\/em>, I am referring to the use of colors to represent aspects of the environment running bash. An ordinary bash prompt may display a username and hostname. Color coding can be used to represent whether the user is root, whether the session is running on a local or remote machine, and other aspects of the environment (not covered in this post).<\/p>\n<p>When running multiple terminals at the same time, with some connected to remote machines and\/or running as root, color coding makes it easier to keep track of the sessions, and may help prevent inadvertently entering a command as root or on the wrong machine.<\/p>\n<p>I have configured my bash prompt&#8217;s colors to be a function of whether I am running as a root or non-root user, and whether I am connected to a local or remote machine. The username is displayed in green for a non-root user and in red for the root user. The hostname is displayed in blue for a local bash session and in cyan for a remote session.<\/p>\n<p>The following image shows the four possible scenarios, 1) on a local machine as a non-root user, 2) on a local machine as root, 3) on a remote machine as a non-root user, and 4) on a remote machine as root.<\/p>\n<p><a href=\"https:\/\/www.dannyadam.com\/blog\/wp-content\/uploads\/2014\/03\/bash.png\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-1459\" src=\"https:\/\/www.dannyadam.com\/blog\/wp-content\/uploads\/2014\/03\/bash.png\" alt=\"bash\" width=\"1264\" height=\"902\" \/><\/a><\/p>\n<p><!--more-->Color coding can be added by putting the following snippet of code in your non-root user&#8217;s .bashrc, both locally and on remote machines. In the session shown in the image above, <em>sudo -s<\/em> was used to become root. Using <em>sudo su<\/em> would not have worked unless the code was also added to root&#8217;s .bashrc or to a global bashrc. By switching to root with\u00a0<em>sudo -s<\/em> or <em>sudo bash<\/em> instead of <em>sudo su<\/em>, root&#8217;s shell will contain the environment provided by the user&#8217;s .bashrc.<\/p>\n<p>Some of the functions for determining whether the current shell is running in an ssh session are from a\u00a0<a href=\"http:\/\/unix.stackexchange.com\/a\/12761\">response<\/a>\u00a0on Unix Stack Exchange. I could not figure out a way to reliably determine whether the running shell is in an ssh session when the shell is running within xterm, so in that case the hostname is displayed in the default color.<\/p>\n<style>.gist table { margin-bottom: 0; }<\/style>\n<div style=\"tab-size: 8\" id=\"gist39501246\" class=\"gist\">\n<div class=\"gist-file\" translate=\"no\" data-color-mode=\"light\" data-light-theme=\"light\">\n<div class=\"gist-data\">\n<div class=\"js-gist-file-update-container js-task-list-container\">\n<div id=\"file-color_bash_prompt-sh\" class=\"file my-2\">\n<div itemprop=\"text\"\n      class=\"Box-body p-0 blob-wrapper data type-shell  \"\n      style=\"overflow: auto\" tabindex=\"0\" role=\"region\"\n      aria-label=\"color_bash_prompt.sh content, created by dstein64 on 02:00PM on September 03, 2016.\"\n    ><\/p>\n<div class=\"js-check-hidden-unicode js-blob-code-container blob-code-content\">\n<p>  <template class=\"js-file-alert-template\"><\/p>\n<div data-view-component=\"true\" class=\"flash flash-warn flash-full d-flex flex-items-center\">\n  <svg aria-hidden=\"true\" height=\"16\" viewBox=\"0 0 16 16\" version=\"1.1\" width=\"16\" data-view-component=\"true\" class=\"octicon octicon-alert\">\n    <path d=\"M6.457 1.047c.659-1.234 2.427-1.234 3.086 0l6.082 11.378A1.75 1.75 0 0 1 14.082 15H1.918a1.75 1.75 0 0 1-1.543-2.575Zm1.763.707a.25.25 0 0 0-.44 0L1.698 13.132a.25.25 0 0 0 .22.368h12.164a.25.25 0 0 0 .22-.368Zm.53 3.996v2.5a.75.75 0 0 1-1.5 0v-2.5a.75.75 0 0 1 1.5 0ZM9 11a1 1 0 1 1-2 0 1 1 0 0 1 2 0Z\"><\/path>\n<\/svg><br \/>\n    <span><br \/>\n      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.<br \/>\n      <a class=\"Link--inTextBlock\" href=\"https:\/\/github.co\/hiddenchars\" target=\"_blank\">Learn more about bidirectional Unicode characters<\/a><br \/>\n    <\/span><\/p>\n<div data-view-component=\"true\" class=\"flash-action\">        <a href=\"{{ revealButtonHref }}\" data-view-component=\"true\" class=\"btn-sm btn\">    Show hidden characters<br \/>\n<\/a>\n<\/div>\n<\/div>\n<p><\/template><br \/>\n<template class=\"js-line-alert-template\"><br \/>\n  <span aria-label=\"This line has hidden Unicode characters\" data-view-component=\"true\" class=\"line-alert tooltipped tooltipped-e\"><br \/>\n    <svg aria-hidden=\"true\" height=\"16\" viewBox=\"0 0 16 16\" version=\"1.1\" width=\"16\" data-view-component=\"true\" class=\"octicon octicon-alert\">\n    <path d=\"M6.457 1.047c.659-1.234 2.427-1.234 3.086 0l6.082 11.378A1.75 1.75 0 0 1 14.082 15H1.918a1.75 1.75 0 0 1-1.543-2.575Zm1.763.707a.25.25 0 0 0-.44 0L1.698 13.132a.25.25 0 0 0 .22.368h12.164a.25.25 0 0 0 .22-.368Zm.53 3.996v2.5a.75.75 0 0 1-1.5 0v-2.5a.75.75 0 0 1 1.5 0ZM9 11a1 1 0 1 1-2 0 1 1 0 0 1 2 0Z\"><\/path>\n<\/svg><br \/>\n<\/span><\/template><\/p>\n<table data-hpc class=\"highlight tab-size js-file-line-container\" data-tab-size=\"4\" data-paste-markdown-skip data-tagsearch-path=\"color_bash_prompt.sh\">\n<tr>\n<td id=\"file-color_bash_prompt-sh-L1\" class=\"blob-num js-line-number js-blob-rnum\" data-line-number=\"1\"><\/td>\n<td id=\"file-color_bash_prompt-sh-LC1\" class=\"blob-code blob-code-inner js-file-line\"># fails to properly return true when running sudo -s<\/td>\n<\/tr>\n<tr>\n<td id=\"file-color_bash_prompt-sh-L2\" class=\"blob-num js-line-number js-blob-rnum\" data-line-number=\"2\"><\/td>\n<td id=\"file-color_bash_prompt-sh-LC2\" class=\"blob-code blob-code-inner js-file-line\">function is_ssh_from_env() {<\/td>\n<\/tr>\n<tr>\n<td id=\"file-color_bash_prompt-sh-L3\" class=\"blob-num js-line-number js-blob-rnum\" data-line-number=\"3\"><\/td>\n<td id=\"file-color_bash_prompt-sh-LC3\" class=\"blob-code blob-code-inner js-file-line\">  if [ -n &quot;${SSH_TTY}&quot; ] || \\<\/td>\n<\/tr>\n<tr>\n<td id=\"file-color_bash_prompt-sh-L4\" class=\"blob-num js-line-number js-blob-rnum\" data-line-number=\"4\"><\/td>\n<td id=\"file-color_bash_prompt-sh-LC4\" class=\"blob-code blob-code-inner js-file-line\">     [ -n &quot;${SSH_CONNECTION}&quot; ] || \\<\/td>\n<\/tr>\n<tr>\n<td id=\"file-color_bash_prompt-sh-L5\" class=\"blob-num js-line-number js-blob-rnum\" data-line-number=\"5\"><\/td>\n<td id=\"file-color_bash_prompt-sh-LC5\" class=\"blob-code blob-code-inner js-file-line\">     [ -n &quot;${SSH_CLIENT}&quot; ]; then<\/td>\n<\/tr>\n<tr>\n<td id=\"file-color_bash_prompt-sh-L6\" class=\"blob-num js-line-number js-blob-rnum\" data-line-number=\"6\"><\/td>\n<td id=\"file-color_bash_prompt-sh-LC6\" class=\"blob-code blob-code-inner js-file-line\">    echo &quot;true&quot;<\/td>\n<\/tr>\n<tr>\n<td id=\"file-color_bash_prompt-sh-L7\" class=\"blob-num js-line-number js-blob-rnum\" data-line-number=\"7\"><\/td>\n<td id=\"file-color_bash_prompt-sh-LC7\" class=\"blob-code blob-code-inner js-file-line\">  else<\/td>\n<\/tr>\n<tr>\n<td id=\"file-color_bash_prompt-sh-L8\" class=\"blob-num js-line-number js-blob-rnum\" data-line-number=\"8\"><\/td>\n<td id=\"file-color_bash_prompt-sh-LC8\" class=\"blob-code blob-code-inner js-file-line\">    echo &quot;false&quot;<\/td>\n<\/tr>\n<tr>\n<td id=\"file-color_bash_prompt-sh-L9\" class=\"blob-num js-line-number js-blob-rnum\" data-line-number=\"9\"><\/td>\n<td id=\"file-color_bash_prompt-sh-LC9\" class=\"blob-code blob-code-inner js-file-line\">  fi<\/td>\n<\/tr>\n<tr>\n<td id=\"file-color_bash_prompt-sh-L10\" class=\"blob-num js-line-number js-blob-rnum\" data-line-number=\"10\"><\/td>\n<td id=\"file-color_bash_prompt-sh-LC10\" class=\"blob-code blob-code-inner js-file-line\">}<\/td>\n<\/tr>\n<tr>\n<td id=\"file-color_bash_prompt-sh-L11\" class=\"blob-num js-line-number js-blob-rnum\" data-line-number=\"11\"><\/td>\n<td id=\"file-color_bash_prompt-sh-LC11\" class=\"blob-code blob-code-inner js-file-line\"> <\/td>\n<\/tr>\n<tr>\n<td id=\"file-color_bash_prompt-sh-L12\" class=\"blob-num js-line-number js-blob-rnum\" data-line-number=\"12\"><\/td>\n<td id=\"file-color_bash_prompt-sh-LC12\" class=\"blob-code blob-code-inner js-file-line\"># fails to properly return true using xterm<\/td>\n<\/tr>\n<tr>\n<td id=\"file-color_bash_prompt-sh-L13\" class=\"blob-num js-line-number js-blob-rnum\" data-line-number=\"13\"><\/td>\n<td id=\"file-color_bash_prompt-sh-LC13\" class=\"blob-code blob-code-inner js-file-line\">function is_ssh_from_who() {<\/td>\n<\/tr>\n<tr>\n<td id=\"file-color_bash_prompt-sh-L14\" class=\"blob-num js-line-number js-blob-rnum\" data-line-number=\"14\"><\/td>\n<td id=\"file-color_bash_prompt-sh-LC14\" class=\"blob-code blob-code-inner js-file-line\">  # method doesn&#39;t work in an xterm so just return false<\/td>\n<\/tr>\n<tr>\n<td id=\"file-color_bash_prompt-sh-L15\" class=\"blob-num js-line-number js-blob-rnum\" data-line-number=\"15\"><\/td>\n<td id=\"file-color_bash_prompt-sh-LC15\" class=\"blob-code blob-code-inner js-file-line\">  if [ -n &quot;${XTERM_SHELL}&quot; ]; then<\/td>\n<\/tr>\n<tr>\n<td id=\"file-color_bash_prompt-sh-L16\" class=\"blob-num js-line-number js-blob-rnum\" data-line-number=\"16\"><\/td>\n<td id=\"file-color_bash_prompt-sh-LC16\" class=\"blob-code blob-code-inner js-file-line\">    echo &quot;false&quot;<\/td>\n<\/tr>\n<tr>\n<td id=\"file-color_bash_prompt-sh-L17\" class=\"blob-num js-line-number js-blob-rnum\" data-line-number=\"17\"><\/td>\n<td id=\"file-color_bash_prompt-sh-LC17\" class=\"blob-code blob-code-inner js-file-line\">    return<\/td>\n<\/tr>\n<tr>\n<td id=\"file-color_bash_prompt-sh-L18\" class=\"blob-num js-line-number js-blob-rnum\" data-line-number=\"18\"><\/td>\n<td id=\"file-color_bash_prompt-sh-LC18\" class=\"blob-code blob-code-inner js-file-line\">  fi<\/td>\n<\/tr>\n<tr>\n<td id=\"file-color_bash_prompt-sh-L19\" class=\"blob-num js-line-number js-blob-rnum\" data-line-number=\"19\"><\/td>\n<td id=\"file-color_bash_prompt-sh-LC19\" class=\"blob-code blob-code-inner js-file-line\"> <\/td>\n<\/tr>\n<tr>\n<td id=\"file-color_bash_prompt-sh-L20\" class=\"blob-num js-line-number js-blob-rnum\" data-line-number=\"20\"><\/td>\n<td id=\"file-color_bash_prompt-sh-LC20\" class=\"blob-code blob-code-inner js-file-line\">  if [[ $(who -m) =~ \\([-:a-zA-Z0-9\\.]+\\)$ ]]; then<\/td>\n<\/tr>\n<tr>\n<td id=\"file-color_bash_prompt-sh-L21\" class=\"blob-num js-line-number js-blob-rnum\" data-line-number=\"21\"><\/td>\n<td id=\"file-color_bash_prompt-sh-LC21\" class=\"blob-code blob-code-inner js-file-line\">    echo &quot;true&quot;<\/td>\n<\/tr>\n<tr>\n<td id=\"file-color_bash_prompt-sh-L22\" class=\"blob-num js-line-number js-blob-rnum\" data-line-number=\"22\"><\/td>\n<td id=\"file-color_bash_prompt-sh-LC22\" class=\"blob-code blob-code-inner js-file-line\">  else<\/td>\n<\/tr>\n<tr>\n<td id=\"file-color_bash_prompt-sh-L23\" class=\"blob-num js-line-number js-blob-rnum\" data-line-number=\"23\"><\/td>\n<td id=\"file-color_bash_prompt-sh-LC23\" class=\"blob-code blob-code-inner js-file-line\">    echo &quot;false&quot;<\/td>\n<\/tr>\n<tr>\n<td id=\"file-color_bash_prompt-sh-L24\" class=\"blob-num js-line-number js-blob-rnum\" data-line-number=\"24\"><\/td>\n<td id=\"file-color_bash_prompt-sh-LC24\" class=\"blob-code blob-code-inner js-file-line\">  fi<\/td>\n<\/tr>\n<tr>\n<td id=\"file-color_bash_prompt-sh-L25\" class=\"blob-num js-line-number js-blob-rnum\" data-line-number=\"25\"><\/td>\n<td id=\"file-color_bash_prompt-sh-LC25\" class=\"blob-code blob-code-inner js-file-line\">}<\/td>\n<\/tr>\n<tr>\n<td id=\"file-color_bash_prompt-sh-L26\" class=\"blob-num js-line-number js-blob-rnum\" data-line-number=\"26\"><\/td>\n<td id=\"file-color_bash_prompt-sh-LC26\" class=\"blob-code blob-code-inner js-file-line\"> <\/td>\n<\/tr>\n<tr>\n<td id=\"file-color_bash_prompt-sh-L27\" class=\"blob-num js-line-number js-blob-rnum\" data-line-number=\"27\"><\/td>\n<td id=\"file-color_bash_prompt-sh-LC27\" class=\"blob-code blob-code-inner js-file-line\"># fails to return true when not running from linux or when connected to a<\/td>\n<\/tr>\n<tr>\n<td id=\"file-color_bash_prompt-sh-L28\" class=\"blob-num js-line-number js-blob-rnum\" data-line-number=\"28\"><\/td>\n<td id=\"file-color_bash_prompt-sh-LC28\" class=\"blob-code blob-code-inner js-file-line\"># screen not started under ssh<\/td>\n<\/tr>\n<tr>\n<td id=\"file-color_bash_prompt-sh-L29\" class=\"blob-num js-line-number js-blob-rnum\" data-line-number=\"29\"><\/td>\n<td id=\"file-color_bash_prompt-sh-LC29\" class=\"blob-code blob-code-inner js-file-line\">function is_ssh_from_ppid() {<\/td>\n<\/tr>\n<tr>\n<td id=\"file-color_bash_prompt-sh-L30\" class=\"blob-num js-line-number js-blob-rnum\" data-line-number=\"30\"><\/td>\n<td id=\"file-color_bash_prompt-sh-LC30\" class=\"blob-code blob-code-inner js-file-line\">  # only works with \/proc directory. otherwise return false<\/td>\n<\/tr>\n<tr>\n<td id=\"file-color_bash_prompt-sh-L31\" class=\"blob-num js-line-number js-blob-rnum\" data-line-number=\"31\"><\/td>\n<td id=\"file-color_bash_prompt-sh-LC31\" class=\"blob-code blob-code-inner js-file-line\">  p=${1:-$PPID}<\/td>\n<\/tr>\n<tr>\n<td id=\"file-color_bash_prompt-sh-L32\" class=\"blob-num js-line-number js-blob-rnum\" data-line-number=\"32\"><\/td>\n<td id=\"file-color_bash_prompt-sh-LC32\" class=\"blob-code blob-code-inner js-file-line\">  if [ -f \/proc\/$p\/stat ]; then<\/td>\n<\/tr>\n<tr>\n<td id=\"file-color_bash_prompt-sh-L33\" class=\"blob-num js-line-number js-blob-rnum\" data-line-number=\"33\"><\/td>\n<td id=\"file-color_bash_prompt-sh-LC33\" class=\"blob-code blob-code-inner js-file-line\">    read pid name x ppid y &lt; &lt;( cat \/proc\/$p\/stat )<\/td>\n<\/tr>\n<tr>\n<td id=\"file-color_bash_prompt-sh-L34\" class=\"blob-num js-line-number js-blob-rnum\" data-line-number=\"34\"><\/td>\n<td id=\"file-color_bash_prompt-sh-LC34\" class=\"blob-code blob-code-inner js-file-line\">    [[ &quot;$name&quot; =~ sshd ]] &amp;&amp; { echo &quot;true&quot;; return; }<\/td>\n<\/tr>\n<tr>\n<td id=\"file-color_bash_prompt-sh-L35\" class=\"blob-num js-line-number js-blob-rnum\" data-line-number=\"35\"><\/td>\n<td id=\"file-color_bash_prompt-sh-LC35\" class=\"blob-code blob-code-inner js-file-line\">    [ &quot;$ppid&quot; -le 1 ] &amp;&amp; { echo &quot;false&quot;; return; }<\/td>\n<\/tr>\n<tr>\n<td id=\"file-color_bash_prompt-sh-L36\" class=\"blob-num js-line-number js-blob-rnum\" data-line-number=\"36\"><\/td>\n<td id=\"file-color_bash_prompt-sh-LC36\" class=\"blob-code blob-code-inner js-file-line\">    is_ssh_from_ppid $ppid<\/td>\n<\/tr>\n<tr>\n<td id=\"file-color_bash_prompt-sh-L37\" class=\"blob-num js-line-number js-blob-rnum\" data-line-number=\"37\"><\/td>\n<td id=\"file-color_bash_prompt-sh-LC37\" class=\"blob-code blob-code-inner js-file-line\">  else<\/td>\n<\/tr>\n<tr>\n<td id=\"file-color_bash_prompt-sh-L38\" class=\"blob-num js-line-number js-blob-rnum\" data-line-number=\"38\"><\/td>\n<td id=\"file-color_bash_prompt-sh-LC38\" class=\"blob-code blob-code-inner js-file-line\">    echo &quot;false&quot;<\/td>\n<\/tr>\n<tr>\n<td id=\"file-color_bash_prompt-sh-L39\" class=\"blob-num js-line-number js-blob-rnum\" data-line-number=\"39\"><\/td>\n<td id=\"file-color_bash_prompt-sh-LC39\" class=\"blob-code blob-code-inner js-file-line\">  fi<\/td>\n<\/tr>\n<tr>\n<td id=\"file-color_bash_prompt-sh-L40\" class=\"blob-num js-line-number js-blob-rnum\" data-line-number=\"40\"><\/td>\n<td id=\"file-color_bash_prompt-sh-LC40\" class=\"blob-code blob-code-inner js-file-line\">}<\/td>\n<\/tr>\n<tr>\n<td id=\"file-color_bash_prompt-sh-L41\" class=\"blob-num js-line-number js-blob-rnum\" data-line-number=\"41\"><\/td>\n<td id=\"file-color_bash_prompt-sh-LC41\" class=\"blob-code blob-code-inner js-file-line\"> <\/td>\n<\/tr>\n<tr>\n<td id=\"file-color_bash_prompt-sh-L42\" class=\"blob-num js-line-number js-blob-rnum\" data-line-number=\"42\"><\/td>\n<td id=\"file-color_bash_prompt-sh-LC42\" class=\"blob-code blob-code-inner js-file-line\"># not totally reliable but works in a lot of cases (may not work in xterms)<\/td>\n<\/tr>\n<tr>\n<td id=\"file-color_bash_prompt-sh-L43\" class=\"blob-num js-line-number js-blob-rnum\" data-line-number=\"43\"><\/td>\n<td id=\"file-color_bash_prompt-sh-LC43\" class=\"blob-code blob-code-inner js-file-line\"># also the functions it relies on don&#39;t return any known false positives,<\/td>\n<\/tr>\n<tr>\n<td id=\"file-color_bash_prompt-sh-L44\" class=\"blob-num js-line-number js-blob-rnum\" data-line-number=\"44\"><\/td>\n<td id=\"file-color_bash_prompt-sh-LC44\" class=\"blob-code blob-code-inner js-file-line\"># only false negatives<\/td>\n<\/tr>\n<tr>\n<td id=\"file-color_bash_prompt-sh-L45\" class=\"blob-num js-line-number js-blob-rnum\" data-line-number=\"45\"><\/td>\n<td id=\"file-color_bash_prompt-sh-LC45\" class=\"blob-code blob-code-inner js-file-line\">function is_ssh() {<\/td>\n<\/tr>\n<tr>\n<td id=\"file-color_bash_prompt-sh-L46\" class=\"blob-num js-line-number js-blob-rnum\" data-line-number=\"46\"><\/td>\n<td id=\"file-color_bash_prompt-sh-LC46\" class=\"blob-code blob-code-inner js-file-line\">  if &quot;$(is_ssh_from_env)&quot; || \\<\/td>\n<\/tr>\n<tr>\n<td id=\"file-color_bash_prompt-sh-L47\" class=\"blob-num js-line-number js-blob-rnum\" data-line-number=\"47\"><\/td>\n<td id=\"file-color_bash_prompt-sh-LC47\" class=\"blob-code blob-code-inner js-file-line\">     &quot;$(is_ssh_from_who)&quot; || \\<\/td>\n<\/tr>\n<tr>\n<td id=\"file-color_bash_prompt-sh-L48\" class=\"blob-num js-line-number js-blob-rnum\" data-line-number=\"48\"><\/td>\n<td id=\"file-color_bash_prompt-sh-LC48\" class=\"blob-code blob-code-inner js-file-line\">     &quot;$(is_ssh_from_ppid)&quot;; then<\/td>\n<\/tr>\n<tr>\n<td id=\"file-color_bash_prompt-sh-L49\" class=\"blob-num js-line-number js-blob-rnum\" data-line-number=\"49\"><\/td>\n<td id=\"file-color_bash_prompt-sh-LC49\" class=\"blob-code blob-code-inner js-file-line\">    echo &quot;true&quot;<\/td>\n<\/tr>\n<tr>\n<td id=\"file-color_bash_prompt-sh-L50\" class=\"blob-num js-line-number js-blob-rnum\" data-line-number=\"50\"><\/td>\n<td id=\"file-color_bash_prompt-sh-LC50\" class=\"blob-code blob-code-inner js-file-line\">  else<\/td>\n<\/tr>\n<tr>\n<td id=\"file-color_bash_prompt-sh-L51\" class=\"blob-num js-line-number js-blob-rnum\" data-line-number=\"51\"><\/td>\n<td id=\"file-color_bash_prompt-sh-LC51\" class=\"blob-code blob-code-inner js-file-line\">    echo &quot;false&quot;<\/td>\n<\/tr>\n<tr>\n<td id=\"file-color_bash_prompt-sh-L52\" class=\"blob-num js-line-number js-blob-rnum\" data-line-number=\"52\"><\/td>\n<td id=\"file-color_bash_prompt-sh-LC52\" class=\"blob-code blob-code-inner js-file-line\">  fi<\/td>\n<\/tr>\n<tr>\n<td id=\"file-color_bash_prompt-sh-L53\" class=\"blob-num js-line-number js-blob-rnum\" data-line-number=\"53\"><\/td>\n<td id=\"file-color_bash_prompt-sh-LC53\" class=\"blob-code blob-code-inner js-file-line\">}<\/td>\n<\/tr>\n<tr>\n<td id=\"file-color_bash_prompt-sh-L54\" class=\"blob-num js-line-number js-blob-rnum\" data-line-number=\"54\"><\/td>\n<td id=\"file-color_bash_prompt-sh-LC54\" class=\"blob-code blob-code-inner js-file-line\"> <\/td>\n<\/tr>\n<tr>\n<td id=\"file-color_bash_prompt-sh-L55\" class=\"blob-num js-line-number js-blob-rnum\" data-line-number=\"55\"><\/td>\n<td id=\"file-color_bash_prompt-sh-LC55\" class=\"blob-code blob-code-inner js-file-line\">function is_xterm() {<\/td>\n<\/tr>\n<tr>\n<td id=\"file-color_bash_prompt-sh-L56\" class=\"blob-num js-line-number js-blob-rnum\" data-line-number=\"56\"><\/td>\n<td id=\"file-color_bash_prompt-sh-LC56\" class=\"blob-code blob-code-inner js-file-line\">  if [ -n &quot;${XTERM_SHELL}&quot; ] || \\<\/td>\n<\/tr>\n<tr>\n<td id=\"file-color_bash_prompt-sh-L57\" class=\"blob-num js-line-number js-blob-rnum\" data-line-number=\"57\"><\/td>\n<td id=\"file-color_bash_prompt-sh-LC57\" class=\"blob-code blob-code-inner js-file-line\">     [ -n &quot;${XTERM_LOCALE}&quot; ] || \\<\/td>\n<\/tr>\n<tr>\n<td id=\"file-color_bash_prompt-sh-L58\" class=\"blob-num js-line-number js-blob-rnum\" data-line-number=\"58\"><\/td>\n<td id=\"file-color_bash_prompt-sh-LC58\" class=\"blob-code blob-code-inner js-file-line\">     [ -n &quot;${XTERM_VERSION}&quot; ]; then<\/td>\n<\/tr>\n<tr>\n<td id=\"file-color_bash_prompt-sh-L59\" class=\"blob-num js-line-number js-blob-rnum\" data-line-number=\"59\"><\/td>\n<td id=\"file-color_bash_prompt-sh-LC59\" class=\"blob-code blob-code-inner js-file-line\">    echo &quot;true&quot;<\/td>\n<\/tr>\n<tr>\n<td id=\"file-color_bash_prompt-sh-L60\" class=\"blob-num js-line-number js-blob-rnum\" data-line-number=\"60\"><\/td>\n<td id=\"file-color_bash_prompt-sh-LC60\" class=\"blob-code blob-code-inner js-file-line\">  else<\/td>\n<\/tr>\n<tr>\n<td id=\"file-color_bash_prompt-sh-L61\" class=\"blob-num js-line-number js-blob-rnum\" data-line-number=\"61\"><\/td>\n<td id=\"file-color_bash_prompt-sh-LC61\" class=\"blob-code blob-code-inner js-file-line\">    echo &quot;false&quot;<\/td>\n<\/tr>\n<tr>\n<td id=\"file-color_bash_prompt-sh-L62\" class=\"blob-num js-line-number js-blob-rnum\" data-line-number=\"62\"><\/td>\n<td id=\"file-color_bash_prompt-sh-LC62\" class=\"blob-code blob-code-inner js-file-line\">  fi<\/td>\n<\/tr>\n<tr>\n<td id=\"file-color_bash_prompt-sh-L63\" class=\"blob-num js-line-number js-blob-rnum\" data-line-number=\"63\"><\/td>\n<td id=\"file-color_bash_prompt-sh-LC63\" class=\"blob-code blob-code-inner js-file-line\">}<\/td>\n<\/tr>\n<tr>\n<td id=\"file-color_bash_prompt-sh-L64\" class=\"blob-num js-line-number js-blob-rnum\" data-line-number=\"64\"><\/td>\n<td id=\"file-color_bash_prompt-sh-LC64\" class=\"blob-code blob-code-inner js-file-line\">\n<\/td>\n<\/tr>\n<tr>\n<td id=\"file-color_bash_prompt-sh-L65\" class=\"blob-num js-line-number js-blob-rnum\" data-line-number=\"65\"><\/td>\n<td id=\"file-color_bash_prompt-sh-LC65\" class=\"blob-code blob-code-inner js-file-line\"># root prompt red (even in ssh session since if you sudo -s in an ssh session<\/td>\n<\/tr>\n<tr>\n<td id=\"file-color_bash_prompt-sh-L66\" class=\"blob-num js-line-number js-blob-rnum\" data-line-number=\"66\"><\/td>\n<td id=\"file-color_bash_prompt-sh-LC66\" class=\"blob-code blob-code-inner js-file-line\"># there seemingly isn&#39;t a way to know you&#39;re in an ssh session)<\/td>\n<\/tr>\n<tr>\n<td id=\"file-color_bash_prompt-sh-L67\" class=\"blob-num js-line-number js-blob-rnum\" data-line-number=\"67\"><\/td>\n<td id=\"file-color_bash_prompt-sh-LC67\" class=\"blob-code blob-code-inner js-file-line\">if [ &quot;root&quot; == &quot;$(whoami)&quot; ]; then<\/td>\n<\/tr>\n<tr>\n<td id=\"file-color_bash_prompt-sh-L68\" class=\"blob-num js-line-number js-blob-rnum\" data-line-number=\"68\"><\/td>\n<td id=\"file-color_bash_prompt-sh-LC68\" class=\"blob-code blob-code-inner js-file-line\">  USER_COLOR=&quot;31m&quot;<\/td>\n<\/tr>\n<tr>\n<td id=\"file-color_bash_prompt-sh-L69\" class=\"blob-num js-line-number js-blob-rnum\" data-line-number=\"69\"><\/td>\n<td id=\"file-color_bash_prompt-sh-LC69\" class=\"blob-code blob-code-inner js-file-line\">else<\/td>\n<\/tr>\n<tr>\n<td id=\"file-color_bash_prompt-sh-L70\" class=\"blob-num js-line-number js-blob-rnum\" data-line-number=\"70\"><\/td>\n<td id=\"file-color_bash_prompt-sh-LC70\" class=\"blob-code blob-code-inner js-file-line\">  USER_COLOR=&quot;32m&quot;<\/td>\n<\/tr>\n<tr>\n<td id=\"file-color_bash_prompt-sh-L71\" class=\"blob-num js-line-number js-blob-rnum\" data-line-number=\"71\"><\/td>\n<td id=\"file-color_bash_prompt-sh-LC71\" class=\"blob-code blob-code-inner js-file-line\">fi<\/td>\n<\/tr>\n<tr>\n<td id=\"file-color_bash_prompt-sh-L72\" class=\"blob-num js-line-number js-blob-rnum\" data-line-number=\"72\"><\/td>\n<td id=\"file-color_bash_prompt-sh-LC72\" class=\"blob-code blob-code-inner js-file-line\">\n<\/td>\n<\/tr>\n<tr>\n<td id=\"file-color_bash_prompt-sh-L73\" class=\"blob-num js-line-number js-blob-rnum\" data-line-number=\"73\"><\/td>\n<td id=\"file-color_bash_prompt-sh-LC73\" class=\"blob-code blob-code-inner js-file-line\"># if you&#39;re running in an xterm, you can&#39;t reliably determine remote status,<\/td>\n<\/tr>\n<tr>\n<td id=\"file-color_bash_prompt-sh-L74\" class=\"blob-num js-line-number js-blob-rnum\" data-line-number=\"74\"><\/td>\n<td id=\"file-color_bash_prompt-sh-LC74\" class=\"blob-code blob-code-inner js-file-line\"># so just use default color<\/td>\n<\/tr>\n<tr>\n<td id=\"file-color_bash_prompt-sh-L75\" class=\"blob-num js-line-number js-blob-rnum\" data-line-number=\"75\"><\/td>\n<td id=\"file-color_bash_prompt-sh-LC75\" class=\"blob-code blob-code-inner js-file-line\">if &quot;$(is_xterm)&quot;; then<\/td>\n<\/tr>\n<tr>\n<td id=\"file-color_bash_prompt-sh-L76\" class=\"blob-num js-line-number js-blob-rnum\" data-line-number=\"76\"><\/td>\n<td id=\"file-color_bash_prompt-sh-LC76\" class=\"blob-code blob-code-inner js-file-line\">  HOST_COLOR=&quot;0m&quot;<\/td>\n<\/tr>\n<tr>\n<td id=\"file-color_bash_prompt-sh-L77\" class=\"blob-num js-line-number js-blob-rnum\" data-line-number=\"77\"><\/td>\n<td id=\"file-color_bash_prompt-sh-LC77\" class=\"blob-code blob-code-inner js-file-line\">elif &quot;$(is_ssh)&quot;; then<\/td>\n<\/tr>\n<tr>\n<td id=\"file-color_bash_prompt-sh-L78\" class=\"blob-num js-line-number js-blob-rnum\" data-line-number=\"78\"><\/td>\n<td id=\"file-color_bash_prompt-sh-LC78\" class=\"blob-code blob-code-inner js-file-line\">  HOST_COLOR=&quot;36m&quot;<\/td>\n<\/tr>\n<tr>\n<td id=\"file-color_bash_prompt-sh-L79\" class=\"blob-num js-line-number js-blob-rnum\" data-line-number=\"79\"><\/td>\n<td id=\"file-color_bash_prompt-sh-LC79\" class=\"blob-code blob-code-inner js-file-line\">else<\/td>\n<\/tr>\n<tr>\n<td id=\"file-color_bash_prompt-sh-L80\" class=\"blob-num js-line-number js-blob-rnum\" data-line-number=\"80\"><\/td>\n<td id=\"file-color_bash_prompt-sh-LC80\" class=\"blob-code blob-code-inner js-file-line\">  HOST_COLOR=&quot;34m&quot;<\/td>\n<\/tr>\n<tr>\n<td id=\"file-color_bash_prompt-sh-L81\" class=\"blob-num js-line-number js-blob-rnum\" data-line-number=\"81\"><\/td>\n<td id=\"file-color_bash_prompt-sh-LC81\" class=\"blob-code blob-code-inner js-file-line\">fi<\/td>\n<\/tr>\n<tr>\n<td id=\"file-color_bash_prompt-sh-L82\" class=\"blob-num js-line-number js-blob-rnum\" data-line-number=\"82\"><\/td>\n<td id=\"file-color_bash_prompt-sh-LC82\" class=\"blob-code blob-code-inner js-file-line\"> <\/td>\n<\/tr>\n<tr>\n<td id=\"file-color_bash_prompt-sh-L83\" class=\"blob-num js-line-number js-blob-rnum\" data-line-number=\"83\"><\/td>\n<td id=\"file-color_bash_prompt-sh-LC83\" class=\"blob-code blob-code-inner js-file-line\">PS1=&quot;\\n\\[\\e[${USER_COLOR}\\]\\u\\[\\e[0m\\]@\\[\\e[${HOST_COLOR}\\]\\h &quot;<\/td>\n<\/tr>\n<tr>\n<td id=\"file-color_bash_prompt-sh-L84\" class=\"blob-num js-line-number js-blob-rnum\" data-line-number=\"84\"><\/td>\n<td id=\"file-color_bash_prompt-sh-LC84\" class=\"blob-code blob-code-inner js-file-line\">PS1+=&quot;\\[\\e[33m\\]\\w\\[\\e[0m\\]\\n\\$ &quot;<\/td>\n<\/tr>\n<\/table>\n<\/div><\/div>\n<\/p><\/div>\n<\/div><\/div>\n<div class=\"gist-meta\">\n        <a href=\"https:\/\/gist.github.com\/dstein64\/c4e879577fea90ecf8e4282ed4cc1879\/raw\/676a431d6b568c7ffd0d216ef1737406aeeb3b9d\/color_bash_prompt.sh\" style=\"float:right\" class=\"Link--inTextBlock\">view raw<\/a><br \/>\n        <a href=\"https:\/\/gist.github.com\/dstein64\/c4e879577fea90ecf8e4282ed4cc1879#file-color_bash_prompt-sh\" class=\"Link--inTextBlock\"><br \/>\n          color_bash_prompt.sh<br \/>\n        <\/a><br \/>\n        hosted with &#10084; by <a class=\"Link--inTextBlock\" href=\"https:\/\/github.com\">GitHub<\/a>\n      <\/div>\n<\/p><\/div>\n<\/div>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>This post explains what I&#8217;m referring to by\u00a0color coded bash prompt, why it is useful to have one, and presents an implementation. By color coded bash prompt, I am referring to the use of colors to represent aspects of the environment running bash. An ordinary bash prompt may display a username and hostname. Color coding [&hellip;]<\/p>\n","protected":false},"author":3,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"jetpack_post_was_ever_published":false,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":true,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"font":"","enabled":false},"version":2}},"categories":[1],"tags":[32,34,35,33],"class_list":["post-1437","post","type-post","status-publish","format-standard","hentry","category-uncategorized","tag-bash","tag-ssh","tag-sysadmin","tag-unix"],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"","jetpack_shortlink":"https:\/\/wp.me\/p1sCC6-nb","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/www.dannyadam.com\/blog\/wp-json\/wp\/v2\/posts\/1437","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.dannyadam.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.dannyadam.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.dannyadam.com\/blog\/wp-json\/wp\/v2\/users\/3"}],"replies":[{"embeddable":true,"href":"https:\/\/www.dannyadam.com\/blog\/wp-json\/wp\/v2\/comments?post=1437"}],"version-history":[{"count":21,"href":"https:\/\/www.dannyadam.com\/blog\/wp-json\/wp\/v2\/posts\/1437\/revisions"}],"predecessor-version":[{"id":25522,"href":"https:\/\/www.dannyadam.com\/blog\/wp-json\/wp\/v2\/posts\/1437\/revisions\/25522"}],"wp:attachment":[{"href":"https:\/\/www.dannyadam.com\/blog\/wp-json\/wp\/v2\/media?parent=1437"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.dannyadam.com\/blog\/wp-json\/wp\/v2\/categories?post=1437"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.dannyadam.com\/blog\/wp-json\/wp\/v2\/tags?post=1437"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}