Skip to main content

If you have been running Linux distributions long, then you know that BASH (Bourne Again SHell) can be found on many of them by default. Arguably the most hardworking part of your system, running as part of so many activities. This includes, development, system configuration, system update, system navigation, the list is rather endless. All one really needs is a terminal to get started on any distribution. This is not a requirement on the “just works” distributions today, yet the missed potential is truly glaring. For the most part, all your applications are doing is running front ends for something that may be a terminal application at heart. For example, Gparted is a GUI Parted, as they are kind enough to show you graphically the commands that are being executed during the process.

On an operating system level, there are three base distributions that require 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 to be installed then you must configure it all in terminal. In Gentoo, you compile everything from scratch and then some. For something like LFS (Linux From Scratch) you will live in a terminal for a minute before you are even finished installing the base system. So, why not learn about how BASH is configured 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?

Bash is included by default in numerous Linux distributions. So, we will just introduce you to the Bourne Again Shell. Bash is a Unix shell and command language written by Brian Fox for the GNU Project as a free software replacement for the Bourne shell, first released in 1989. So, it is not only a way to talk to your system, you can write helpful scripts to help you preform the task. This way you do not have to type them out by hand. Perhaps you want to automate a scripted task. Furthermore, you can set a script to your crontab, repeating said script whenever desired. So 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 just 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

Alternatively, you can 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. So, 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. Putting a magnifying glass on where you are located in the file system. For a couple of very useful commands, try these out with the && operator adding the commands in succession.

date && cal

Now you not only know what time it is, but the day of the month.

Alias

Now in order 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 editor you like. We will use Vim since it is available on many systems.

vim ~/.bashrc
Default bashrc in Linux

Since we used the short 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 move everywhere. Rather you can bring it to you, like a BASH warp drive bringing the directories to you. Alternatively, you can go anywhere you need to till you get comfortable. Now let’s add a few short but useful alias.

# 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'
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

Now for the most fun part ever to make yourself look even more on edge. Customizing your command prompt to show details about things that are important, or perhaps obscure your username from passersby with some style. Let us 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 hostname 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╰─➩ '
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 right 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
Linux bashrc git detection

The way these are written 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 just with bash. Next shell we will talk about is Zsh. That is a newer and with so much customization possible. You can add cool plugins. It even comes on Manjaro. We hope you enjoy altering your command prompt. Try out alias and also fun commands. There is nothing stopping you from making a work environment that you enjoy. We will also learn to customize editors like Vim and even Nano. Thanks for reading!

Leave a Reply