Bash: Handy bashrc Bits

Below are some handy modifications I have made to my .bashrc file over the years.

Helpful Aliases

The following two aliases just ensure grep and ls output with color. Some distros don't set this by default, which is a bit annoying, so I just set it myself.

alias ls='ls --color=auto'
alias grep='grep --color=auto'

This one is really only useful if you create QR codes with your computer somewhat often. I often use QR codes to share URLs with my phone, or text I don't want to type, passwords, etc. I used to run qrencode saving to an image file, then open the image file, then scan with my phone. This skips that process by using unicode text output to write the QR code to stdout in your terminal. So cool! Give this a try with qrencode 'Hello world!'.

alias qrencode='qrencode -t ansiutf8'

This one will only be useful for folks who often use mplayer. Every now and then, I get a 4k video which is more than a single core of my laptop can handle, downscaling on the fly to fit my 1080p monitor. This sets mplayer to use 4 threads always so I don't have to remember to do that. It also sets the cache size to 32 MB, which avoids a number of streaming problems over slow connections. It also sets mplayer to quiet, so the output isn't quite as verbose as it normally is.

alias mplayer='mplayer -quiet -lavdopts threads=4 -cache 32768'

Custom PS1 Line

Below is the shell code that generates the value for my PS1 variable, every time I run a command. If you put this in your bashrc file, every time you run a command you will see the exit code for the previous command, followed by [user@hostname cur_dir]$. Basically it's a standard PS1, but with the exit code from the previous command, nothing more.

A good way to test this out is to paste the below into your terminal (bash will load it line by line), then press ctrl + c, which should show you something like 130 [user@host ~]$

This is really handy if you're the kind of person who gets annoyed every time they have to type echo $?, and they do it a lot.

Also note:

  • If the exit code is 1, the 1 is colored red

  • If the exit code is 2 (often file not found), the 2 is colored yellow

  • Otherwise it is colored whatever your Xresources defines brightcyan as

# Sets the PS1. Different every time based on the previous command's exit code
get_prompt() {
  ps='[\[\e[1;36m\]\u\[\e[0m\]@\[\e[1;34m\]\h\[\e[0m\] \W]\$ '
  if [ "${ecode}" -eq 1 ]; then
    PS1="\[\e[1;31m\]${ecode}\[\e[0m\] ${ps}"
  elif [ "${ecode}" -eq 2 ]; then
    PS1="\[\e[1;33m\]${ecode}\[\e[0m\] ${ps}"
    PS1="\[\e[1;36m\]${ecode}\[\e[0m\] ${ps}"
export PROMPT_COMMAND=get_prompt

New or Attach Tmux Session

I pretty much live in a tmux session nowadays with only one terminal emulator open (xterm most of the time). If you're one of those folks, you might find this to be useful.

If you put this in your .bashrc file, a new shell will create a tmux session. If you close this window, tmux is of course still running in the background. Reopening a new terminal emulator will detect the previously running tmux session and re-attach to it, rather than opening a new one.

# Only run these operations if we are not inside a tmux session already
if [ -z "${TMUX}" ]; then
  if tmux list-sessions 2>&1 1>/dev/null; then
    printf "Existing tmux session found. Attaching\n"
    tmux attach
    # Enter new session

Last edited: 2020-12-29 00:54:32 UTC