Skip to main content

Customizing the Bash shell

Why settle for boring Bash? You can customize every aspect of your shell and this article gets you started.
Image
sea shells in a window

Photo by Caio Resende from Pexels

The Bash shell is a command processing environment that many sysadmins and developers use every day. It was first developed in 1989 and has been the default shell for many distributions since Linux was created. In most cases, Bash or 'Bourne-again shell' is the framework that you are working inside of every time you open the command line. It also has its own scripting language and boasts a huge number of utilities for those with the mental discipline to learn them.

Why would you customize your shell?

The Bash shell offers a ton of utility, but it’s not a perfect system. Throughout the years, command-line enthusiasts have been finding flaws and making improvements in various shells, and Bash is no exception. There are tons of additions that you can make to Bash. Everything from meaningful functionality changes to shortcuts and aesthetic changes made according to your personal tastes. That said, everyone has their own preferred flavor of aesthetics, so I am going to focus on changes to functionality.

Start small

The easiest way to get started with customizing your shell is to find out where most of your command line time is spent. Do you find yourself using grep and netstat often? Are you moving around the filesystem countless times per hour? If so, how can you make these tasks more efficient? In the first article I ever wrote for this site, I talked about an automation script called AutoHotkey. Unfortunately, it only runs on Windows. I was challenged by a reader to find something he could use for Linux to accomplish the same tasks. While there isn’t a neatly packaged solution to that challenge, you can create an alias for modified commands inside your shell to the same effect. While this can be a little time consuming, if you take the time to learn about aliasing and create some of your own, you will be a smarter and more efficient sysadmin for it.

Rick Greene, an Enable Sysadmin community member, shared one of his favorite aliases, where he elevates to the root shell by using sudo.

alias root="sudo su - root"

Effectively, he works as the root user while keeping track of his actions in the system using his own user account password. While the preferred method here would be to use the sudo command prior to any action, the beauty of this example is that Bash allows each user to create a solution that works for them! You can alias virtually every command in your shell. I recommend that you try to create a few of your own aliases now.

[ For information on creating aliases in Linux, check out my previous article. ]

X > f(x)

If you are mathematically inclined, you already know what we are talking about here. Functions have made their way out of your classroom nightmares and into your work life! The good news here is that these are pretty straightforward and can be very useful. They allow us a bridge between aliasing and full-blown scripting. The big takeaway here is that you can chain multiple commands together. Let’s look at the syntax for creating these functions and a practical example for use. To create a function, use the following syntax:

function_name () {command1; command2;}
#You MUST use a semicolon after each command, including the last. 

How would we use this? One of the best examples I have seen floating around the web is a function allowing you to create a new directory and then immediately move into that directory. That function would look something like this:

mkd () {mkdir -p $1; cd $1;}

Now, if we create a directory called func_test using the mkd function, we can see that it automatically places us into the new directory.

# mkd func_test
# pwd
# Home/user/func_test

History repeats itself? Only if you want...

Before writing this article, I had reached out to our contributor community for feedback on their favorite Bash improvements. There was a common theme in all of the feedback I received. People want to change the way that Bash history operates. One of the most helpful changes that I saw was the ability to remove duplicate entries from history.

Seth Kenlon of Opensource.com sent over the following from his .bashrc file. It allows history to ignore duplicate entries and commands that begin with a space.

"export HISTCONTROL=$HISTCONTROL:ignorespace:ignoredups"

Red Hatter Steve Newsted pointed me to a great thread on StackExchange, where a user was looking to alter the Bash history to remember everything from every terminal on his machine while having that history be accessible from every terminal on said machine. Check out the creativity of the answer by suntzuisafterU. Just add the following to your ~/.bashrc file.

#Avoid duplicates
HISTCONTROL=ignoredups:erasedups  
# When the shell exits, append to the history file instead of overwriting it
shopt -s histappend

# After each command, append to the history file and reread it
PROMPT_COMMAND="${PROMPT_COMMAND:+$PROMPT_COMMAND$'\n'}history -a; history -c; history -r"

Next steps

Now you can see the plethora of changes, optimizations, and personalizations that are available in Bash. This article only scratches the surface of what you can do within the shell. Get out there and explore it for yourself! If you have a favorite Bash addition, feel free to send it over to the team at enable-sysadmin@redhat.com. I would love to see some of the creative solutions that our readers are putting to use!

[ Need more help getting started with Linux system administration? Take a Red Hat system administration course. ]

Topics:   Linux  
Author’s photo

Tyler Carrigan

Tyler is the Sr. Community Manager at Enable Sysadmin, a submarine veteran, and an all-round tech enthusiast! He was first introduced to Red Hat in 2012 by way of a Red Hat Enterprise Linux-based combat system inside the USS Georgia Missile Control Center. More about me

Try Red Hat Enterprise Linux

Download it at no charge from the Red Hat Developer program.