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.

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.

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.

[ -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.

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.

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.

# 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 — Article

More Content


On the Web