Tweaking the Bash Shell

Bourne-again shell
Bourne-Again Shell

The Bourne Again fish and zsh are popular alternative shells. is ubiquitous among many unix-like systems. Here’s a few tweaks that are applied to my bash configuration for My preference is to not add too many tweaks (like aliases) to avoid building up bad muscle memory. interactivity.

Conditional Prompt Colors

The color This acts as a guide when ascending and descending through shells. in the prompt gives a visual cue for the current environment. Green is indicative of a secure shell connection (SSH), red of root privilege, blue of the standard user, and white (an unmodified prompt) of a foreign system.

Prompt Colors in Bash
Prompt color changes based on the current context

This is achieved by short circuiting the $PS1 (the primary My preferred PS1 is not particularly verbose. variable) in ~/.bash_profile.

bash
PS1_USER='\[\e[0;34m\]\W\[\e[0m\] \[\e[0;34m\]\$\[\e[0m\] '
PS1_ROOT='\[\e[0;31m\]\W\[\e[0m\] \[\e[0;31m\]\$\[\e[0m\] '
PS1_SSHD='\[\e[0;32m\]\W\[\e[0m\] \[\e[0;32m\]\$\[\e[0m\] '

[ "$EUID" != 0 ] && export PS1="$PS1_USER";
[ "$EUID" = 0 ] && export PS1="$PS1_ROOT";
[ -n "$SSH_CLIENT" ] && [ "$EUID" != 0 ] && export PS1="$PS1_SSHD";

Exit Codes

Often there is a need to know the return or exit status of the last command. This is extremely useful in varied situations. The variable $? contains the exit status of the last command.

Prompt Return Codes
Prompt shows the exit status of the last command

In ~/.bash_profile, the logic $(E=$? && [ "$E" != 0 ] && echo "$E ") is prefixed to the $PS1. The exit status will be printed on a carriage return.

bash
PS1_USER='$(E=$? && [ "$E" != 0 ] && echo "$E ")\[\e[0;34m\]\W\[\e[0m\] \[\e[0;34m\]\$\[\e[0m\] '
PS1_ROOT='$(E=$? && [ "$E" != 0 ] && echo "$E ")\[\e[0;31m\]\W\[\e[0m\] \[\e[0;31m\]\$\[\e[0m\] '
PS1_SSHD='$(E=$? && [ "$E" != 0 ] && echo "$E ")\[\e[0;32m\]\W\[\e[0m\] \[\e[0;32m\]\$\[\e[0m\] '

[ "$EUID" != 0 ] && export PS1="$PS1_USER";
[ "$EUID" = 0 ] && export PS1="$PS1_ROOT";
[ -n "$SSH_CLIENT" ] && [ "$EUID" != 0 ] && export PS1="$PS1_SSHD";

Fuzzy Finder

The command line fuzzy finder fzf has keybindings for common operations in bash and zsh. This gives the The terminal emulator in use is urxvt. Simple Terminal (st) is also rather delightful and popular among the purists. a more opinionated reverse lookup (CTRL+R) workflow.

Command Line Fuzzy Finder
Command Line Fuzzy Finder

Source the fuzzy finder’s keybindings in the ~/.bashrc configuration. This only works if the command fzf has been installed on the These paths are derived from Debian and Arch based systems.

bash
[ -f '/usr/share/fzf/completion.bash' ] && . /usr/share/fzf/completion.bash
[ -f '/usr/share/fzf/key-bindings.bash' ] && . /usr/share/fzf/key-bindings.bash
[ -f '/usr/share/doc/fzf/examples/completion.bash' ] && . /usr/share/doc/fzf/examples/completion.bash
[ -f '/usr/share/doc/fzf/examples/key-bindings.bash' ] && . /usr/share/doc/fzf/examples/key-bindings.bash

Vi Mode

Bash has a built in readline vi mode. This removes the tempting dependency on the mouse for precise text manipulation. The prefixed + and : You can use anything you want. indicate insert and normal modes respectively. Pressing v in normal mode invokes vim on the current command.

Vi Mode
Vi Mode

Use the set command in the ~/.inputrc Create this file inside the user’s home directory. to enable showing the vi mode prompt.

bash
set show-mode-in-prompt on
set vi-ins-mode-string "+ "
set vi-cmd-mode-string ": "

Use the set command to enable vi There is also an emacs mode. by adding the directive in ~/.bashrc.

bash
set -o vi;

Readline Configuration

The shell’s readline can be configured in the ~/.inputrc file. Here’s a few other readline tweaks for a sane setup.

bash
# Set colors on completion results.
set colored-stats on

# Ignore case when using tab completion.
set completion-ignore-case on

# Show completion results on the first tab press.
set show-all-if-ambiguous on

# Avoid inserting tab completions in the middle of a word.
set skip-completed-text on

# Up and down reverse search will consider the currently typed command.
"\e[A": history-search-backward
"\e[B": history-search-forward
29 April 2020 — Written
27 May 2020 — Updated
Thedro Neely — Creator
tweaking-the-bash-shell.md — Article

More Content

Openring

Web Ring

Comments

References

  1. https://thedroneely.com/git/
  2. https://thedroneely.com/
  3. https://thedroneely.com/posts/
  4. https://thedroneely.com/projects/
  5. https://thedroneely.com/about/
  6. https://thedroneely.com/contact/
  7. https://thedroneely.com/abstracts/
  8. https://ko-fi.com/thedroneely
  9. https://thedroneely.com/tags/bash/
  10. https://thedroneely.com/posts/tweaking-the-bash-shell/#isso-thread
  11. https://thedroneely.com/posts/rss.xml
  12. https://thedroneely.com/images/tweaking-the-bash-shell.png
  13. https://tiswww.case.edu/php/chet/bash/bashtop.html
  14. https://fishshell.com/
  15. https://ohmyz.sh/
  16. https://thedroneely.com/posts/tweaking-the-bash-shell/#conditional-prompt-colors
  17. https://thedroneely.com/images/bash-tweaks-color-prompts.gif
  18. https://thedroneely.com/posts/tweaking-the-bash-shell/#code-block-60ee09d
  19. https://thedroneely.com/posts/tweaking-the-bash-shell/#exit-codes
  20. https://thedroneely.com/images/bash-tweaks-return-codes.gif
  21. https://thedroneely.com/posts/tweaking-the-bash-shell/#code-block-f9491f7
  22. https://thedroneely.com/posts/tweaking-the-bash-shell/#fuzzy-finder
  23. https://github.com/junegunn/fzf
  24. https://github.com/junegunn/fzf#key-bindings-for-command-line
  25. https://st.suckless.org/
  26. https://thedroneely.com/images/bash-tweaks-fzf.gif
  27. https://thedroneely.com/posts/tweaking-the-bash-shell/#code-block-7ff0adb
  28. https://thedroneely.com/posts/tweaking-the-bash-shell/#vi-mode
  29. https://thedroneely.com/images/bash-tweaks-vi-mode.gif
  30. https://thedroneely.com/posts/tweaking-the-bash-shell/#code-block-57894bb
  31. https://thedroneely.com/posts/tweaking-the-bash-shell/#code-block-cf6f740
  32. https://thedroneely.com/posts/tweaking-the-bash-shell/#readline-configuration
  33. https://thedroneely.com/posts/tweaking-the-bash-shell/#code-block-d8163ff
  34. https://www.thedroneely.com/posts/tweaking-the-bash-shell.md
  35. https://thedroneely.com/posts/programming-sans-internet/
  36. https://thedroneely.com/posts/site-updates-navigation/
  37. https://thedroneely.com/posts/my-ts100-settings-and-configuration/
  38. https://git.sr.ht/~sircmpwn/openring
  39. https://drewdevault.com/2022/11/12/In-praise-of-Plan-9.html
  40. https://drewdevault.com/
  41. https://mxb.dev/blog/the-indieweb-for-everyone/
  42. https://mxb.dev/
  43. https://www.taniarascia.com/simplifying-drag-and-drop/
  44. https://www.taniarascia.com/
  45. https://thedroneely.com/posts/tweaking-the-bash-shell#isso-thread
  46. https://thedroneely.com/posts/tweaking-the-bash-shell#conditional-prompt-colors
  47. https://thedroneely.com/posts/tweaking-the-bash-shell#code-block-60ee09d
  48. https://thedroneely.com/posts/tweaking-the-bash-shell#exit-codes
  49. https://thedroneely.com/posts/tweaking-the-bash-shell#code-block-f9491f7
  50. https://thedroneely.com/posts/tweaking-the-bash-shell#fuzzy-finder
  51. https://thedroneely.com/posts/tweaking-the-bash-shell#code-block-7ff0adb
  52. https://thedroneely.com/posts/tweaking-the-bash-shell#vi-mode
  53. https://thedroneely.com/posts/tweaking-the-bash-shell#code-block-57894bb
  54. https://thedroneely.com/posts/tweaking-the-bash-shell#code-block-cf6f740
  55. https://thedroneely.com/posts/tweaking-the-bash-shell#readline-configuration
  56. https://thedroneely.com/posts/tweaking-the-bash-shell#code-block-d8163ff
  57. https://thedroneely.com/posts/kubernetes-in-a-linux-container/
  58. https://thedroneely.com/posts/extreme-ssh-hardening/
  59. https://thedroneely.com/posts/my-sublime-text-setup/
  60. https://thedroneely.com/posts/trying-out-this-website/
  61. https://thedroneely.com/posts/a-javascript-executable/
  62. https://drewdevault.com/2022/09/16/Open-source-matters.html
  63. https://mxb.dev/blog/make-free-stuff/
  64. https://thedroneely.com/sitemap.xml
  65. https://thedroneely.com/index.json
  66. https://thedroneely.com/resume/
  67. https://gitlab.com/tdro
  68. https://github.com/tdro
  69. https://codeberg.org/tdro
  70. https://thedroneely.com/analytics
  71. https://thedroneely.com/posts/tweaking-the-bash-shell#
  72. https://creativecommons.org/licenses/by-sa/2.0/
  73. https://thedroneely.com/git/thedroneely/thedroneely.com
  74. https://opensource.org/licenses/GPL-3.0
  75. https://www.thedroneely.com/
  76. https://thedroneely.com/posts/tweaking-the-bash-shell/#