Greatly Customizing Your bashrc File In Linux!

Table of Contents

If you have been running Linux distributions long, then you know of the Bourne Again SHell (bash). bash is on many distributions by default and is configured and managed by its bashrc file. bash is the hardest working part of your system, running as part of so many activities. This includes: development, system configuration, updates, and system navigation, the list is rather endless. All one needs is a terminal to get started on any distribution. This is not the focus of the “just works” distributions of today and the missed potential is glaring. For the most part, all your applications are doing is running a front end. As most apps may be a terminal application at heart. For example, Gparted is a GUI for parted. They are kind enough to show you the commands that are being executed via the UI during the process.

At an OS level, there are three base distributions that need some terminal skills to use. Tools to partition the drives or something like the famous gcc GNU compiler. For instance, Arch requires you to partition your drive to make way for the base system. The rest of the installation gets handled in terminal. In Gentoo, you compile everything from scratch and then some. For something like Linux From Scratch (LFS) you will live in a terminal for a minute. That’s all before you are even finished installing the base system. So, why not learn about how to configure bash in a file called .bashrc right in your home folder? From theme customizing, to useful features you need. Let us get lost in the terminal. Let the walls melt away, feel the ASCII characters flow through you!

So what is BASH?

As stated above bash comes by default on various Linux distributions. bash is a Unix shell and command language written by Brian Fox for the GNU Project. It is a free software replacement for the Bourne shell, first released in 1989. It is not only a way to talk to your system, but you can write helpful scripts to help you preform tasks. This way you do not have to type them out by hand. You want to automate a scripted task? bash can help do that for you. Furthermore, you can even set a script to be ran via crontab. That way it repeats said script whenever desired. As you can see, it is a language onto itself, with lots of functionality. Let’s get right into the basics you will need to get around your system and see where you are going. To see what is in your current directory, try the following:
				
					ls
				
			

You can also direct ls to any directory you are trying to see the contents of by adding the path.

				
					ls /etc
				
			

You can use / to show you files and directories in the root directory. To look in the home directory, you can be general or specific like so.

				
					ls /home/yourUserName/Downloads
				
			
You can also write the same thing much shorter:
				
					ls ~/Downloads
				
			

Some applications want you to write a full path like the former. So, it is good to know how. You can pretty much stay put and look around. You might decide that you want to move around to see what is going on. For this use something like this:

				
					cd ~/Downloads
				
			
Now you can see and navigate. Very much the most basic and important commands to start with. You can also find your relative path like so:
				
					pwd
				
			

This will give you the path all the way to the home directory. Think of it as putting a magnifying glass on where you currently are in the file system. For a couple of very useful commands, try these out with the && operator. Then add any following commands in succession.

				
					date && cal
				
			
Now you not only know what time it is, but the day of the month as well.

Bashrc Aliases

Now if you wanted to look like a hacker that is working very fast and seeming without care of keys pushed. You only need-know of one important function that your bashrc file can bring you, alias. To edit your .bashrc you can use any text editor you like. We will use vim since it is available on many systems.

				
					vim ~/.bashrc
				
			
bashrc-vanilla-arch
Default bashrc in Linux

Using shortcut for home: (~), we can execute this command anywhere in the file system and get to the file we wish to edit. Remember this when navigating as you may not need to actually be inside the directory to edit a file there. Rather you can bring it to you, like a warp drive bringing the directories to you. You can go anywhere without leaving the directory you are in. Now let’s add a few short but useful aliases.

				
					# Arch
alias s=sudo # Alias for common commands
alias v=vim
alias vi=vim
alias h=htop # Alias for much used applications
alias n=neofetch
alias temp=sensors
alias SV='sudo vim' # Alias for command stings
alias sour='source ~/.bashrc'
alias PC='sudo pacman -Scc --noconfirm'
alias U='sudo pacman -Syu --noconfirm'

# Debian
alias s=sudo # Alias for common commands
alias v=vim
alias vi=vim
alias h=htop # Alias for much used applications
alias n=neofetch
alias temp=sensors
alias SV='sudo vim' # Alias for command stings
alias sour='source ~/.bashrc'
alias R='sudo shutdown -r now'
alias U='sudo apt update && sudo apt upgrade -y && sudo apt autoremove -y'
				
			

.bashrc is a dotfile that lives in your home directory. dotfiles are normally hidden from the ls command. In order to see it use the ls -la command to see hidden files.

bashrc-alias
Add aliases in bashrc

So now you can see we added alias examples to update on Arch and Debian systems. You can adapt that to any package manager. Now we can go ahead and use Esc to close the file, then type :wq to write and quit the file.

Custom Prompt In Bashrc

Now for the most fun part ever to make yourself look even more edgy: Customizing your command prompt. From showing important details to obscuring your username from passersby. Lets get back into our .bashrc file.

				
					vim ~/.bashrc
				
			

Now we can comment out the current line starting with PS1 with a #. Then we can add the code below to change the prompt. We will be letting go of the host name and username. We will also be adding custom exit code readout.

				
					# If not running interactively, don't do anything
[[ $- != *i* ]] && return
#
alias ls='ls --color=auto'
#PS1='[\u@\h \W]\$ '
#
# Custom exit code readout for errors
#
PS1='╭🖥─\e[0;32m(\w)\e[01;35m$(code=${?##0};echo ${code:+─💢${code}💢})\e[00m\n╰─➩ '
				
			
bashrc-custom-with-error-code
Custom bash prompt in Linux bashrc

Now you may wonder how to add git repository functionality. We will give you a what you need right here to show what you have going on in your terminal.

				
					# If not running interactively, don't do anything
[[ $- != *i* ]] && return
#
alias ls='ls --color=auto'
#PS1='[\u@\h \W]\$ '
#
# Custom exit code readout for errors
#
#PS1='╭🖥─\e[0;32m(\w)\e[01;35m$(code=${?##0};echo ${code:+─💢${code}💢})\e[00m\n╰─➩ '
#
# GIT DETECTION IN TERMINAL 
# Settings:
BGP_BRANCH_SAFE=("develop" "dev")
BGP_BRANCH_UNSAFE=("master")
BGP_USER_UNSAFE=("root" "prod" "git")

function in_array() {
  local e
  for e in "${@:2}"; do [[ "$e" == "$1" ]] && return 0; done
  return 1
}

function prompt() {

  # Define colors
  local RED="\[\e[31m\]"
  local YELLOW="\[\e[33m\]"
  local GREEN="\[\e[32m\]"
  local RESET="\[$(tput sgr0)\]"

  # Get git status
  local GIT_STATUS=$(git status --porcelain --ignore-submodules 2> /dev/null | wc -l)
  local GIT_STATUS_PS="$GREEN$RESET"
  if [[ "0" != "$GIT_STATUS" ]]; then
    GIT_STATUS_PS="$RED~$GIT_STATUS$RESET"
  fi

  # Get git branch
  local GIT_BRANCH=$(git branch 2> /dev/null | sed -e '/^[^*]/d' -e 's/* \(.*\)/\1/')
  local GIT_BRANCH_PS="$YELLOW─($GIT_BRANCH)$RESET$GIT_STATUS_PS"
  if in_array "${GIT_BRANCH}" "${BGP_BRANCH_SAFE[@]}"; then
    GIT_BRANCH_PS="$GREEN─($GIT_BRANCH)$RESET$GIT_STATUS_PS"
  elif in_array "${GIT_BRANCH}" "${BGP_BRANCH_UNSAFE[@]}"; then
    GIT_BRANCH_PS="$RED─($GIT_BRANCH)$RESET$GIT_STATUS_PS"
  elif [[ "" == "$GIT_BRANCH" ]]; then
    GIT_BRANCH_PS=""
  fi

  PS1="╭🖥─${GREEN}(\w)${RESET}${GIT_BRANCH_PS}\n╰─➩ "
}

PROMPT_COMMAND=prompt
				
			
bashrc-git-prompt
Linux bashrc git detection

Due to the way the options are in this file, you want to avoid using them all at once. It is much better to either comment them out like we have here or clean out your .bashrc. Always keep the default in case you feel like going back. Now you can see the possibilities with bash. We hope you enjoy altering your command prompt. Feel free to try out aliases and some fun commands. There is nothing stopping you from making a work environment that you enjoy. Next shell we will talk about is Zsh. That is newer shell with so much more customization possible. You can add cool plugins and it even ships with Manjaro. We will also learn to customize editors like vim and even nano. Until next time, thanks for reading!

Meet the Author

Leave a Reply