My preferred Bash prompt:
Consider the default CentOS / RHEL Bash prompt:
[username@hostname ~]$
... or under Debian / Ubuntu (some versions put a space before the dollar prompt):
username@hostname:~$
Compared to a colored, 2-line prompt (colors not shown here or elsewhere due to Markdown limitations):
username@hostname:~
$
Advantages of the last:
-
Allows for more room for command entry.
-
Command entry always starts at the same column (3) - regardless of the length of the current path (
$PWD
). -
Provides clear separation between commands - due to both the color and the newline before each prompt, as well as adding distinction between the prompt and the command entry.
-
Follows the same semantics already set by default in
$PROMPT_COMMAND
to set the window title. -
Makes it easier to use SCP, rsync, etc., between hosts - as the prompt follows the identical semantics required by these programs, allowing for this portion to be simply copied & pasted:
[[user@]host1:]file1
Continue reading below for the full version. However, for a basic version as a one-liner:
-
Simply run the following - or include near the end of
~/.bashrc
:PS1='\n\[\e[32m\]\u@\h\[\e[0m\]:\[\e[33m\]\w\[\e[0m\]\n\$ '
-
For a root Bash prompt, the syntax is the same as above - but changes the username & host color from green to red. In
/root/.bashrc
:PS1='\n\[\e[31m\]\u@\h\[\e[0m\]:\[\e[33m\]\w\[\e[0m\]\n\$ '
- Bash will automatically substitute a
#
for the$
(dollar prompt) for the root user.
- Bash will automatically substitute a
$?: 1
2019-05-12 01:39:29-05:00 up 30 days, 7:27, 1 user 0.00 0.00 0.00 1/196
mark@localhost:~
$
- The first line (as shown) displays the return code from the last commmand, but only if non-zero.
- This is shown on its own line, using black test on a yellow background.
- Typically this is something that needs attention, as it usually means the previous command returned something other than a "success".
- A similiar approach for this is further detailed at https://blog.superuser.com/2011/09/21/customizing-your-bash-command-prompt/.
- The next line shows in blue (low contrast, as to not be too obtrusive):
- The current date and time.
- Helpful for keeping track of how long commands took to execute - and when looking back, when they were executed.
- The date/time format is in ISO 8601 format (technically RFC 3339), the only correct way to write numeric dates. (An Internet and global standard, unambiguous, provides for proper sorting, etc.)
- The system uptime (reformatted from
uptime
) and current number of users. - The load average (from
/proc/loadavg
).- Helpful as an indicator if the system load starts spiking while you're working on the system - also something that may require attention!
- The current date and time.
- The last 2 lines remain exactly as outlined in the basic example above.
I typically run my terminals at 120 characters - but this still fits nicely in a default 80-column window. (The longest line is 75 characters as-shown, while still allowing for any needed overflow, such as 100+ day uptimes.)
-
Copy
maz-bash-prompt.sh
to an appropriate location for use.- This could be under
/etc
, or~
(the home directory).
- This could be under
-
Source (
source
or.
) the output of the script. For example:. <(/etc/maz-bash-prompt.sh)
or
. <(~/maz-bash-prompt.sh)
-
For persistent use, append the same command used above to the end of your
~/.bashrc
.
- Only supported by Bash (not ash, csh, dash, ksh, sh, tcsh, zsh, etc.)
- When run under Cygwin (where
$OSTYPE
starts withcygwin
), the uptime and load averages are not included by default due to specific performance concerns (as commented within the script). - Does not (yet) include support for including an indicator from any
/etc/debian_chroot
.
Bug reports and pull requests are welcome on GitHub at https://github.com/ziesemer/maz-bash-prompt.