Bash - Tweaks and Tuning

GNU Bash can be considered the standard shell in GNU/Linux systems. But, do you know that you tune your Bash to allow case-insensitive completion or create your own commands? In this article, I will show some ways to make Bash "your Bash" and more useful.

Bash - Tweaks and Tuning

GNU Bash can be considered the standard shell in GNU/Linux systems. But, do you know that you tune your Bash to allow case-insensitive completion or create your own commands? In this article, I will show some ways to make Bash "your Bash" and more useful.

GNU Bash

Even new users of GNU/Linux based distributions are aware, that there is a shell. In some cases, this is needed to do fine-tuning to your system. It is also a very nifty way to provide guides and configurations without putting tons of screenshots together.

On most GNU/Linux based distributions, you will find GNU Bash (Bash for all future mentions) as the default shell. The GNU Project itself also has an introduction to Bash as cited below.

Bash is the GNU Project's shell—the Bourne Again SHell. This is an sh-compatible shell that incorporates useful features from the Korn shell (ksh) and the C shell (csh).  It is intended to conform to the IEEE POSIX P1003.2/ISO 9945.2 Shell and Tools standard.  It offers functional improvements over sh for both programming and interactive use.  In addition, most sh scripts can be run by Bash without modification.

Tuning and Tweaks

I am using Bash for quite some time now. In many articles, you will also get an idea that there are other shells like ZSH or Fish. And very often, these are recommended to people, for simple changes, that can also be made in Bash.

Let's see how we can adjust Bash and decide afterwards, if we really want to replace the shell.

Hint
The guide is tested on Fedora 35 with Bash 5.1.8.

Configuration files

Tuning and tweaking bash can be done in multiple places. Furthermore, there are differences of the implementation, that are different from distribution to distribution. Some GNU/Linux distributions are differentiating between login shells and interactive shells, some are not. In case something is not 100% working for you, please check the Docs & Links section, where I have put some articles.

For this tutorial (and the coming articles), I am sticking to the below files and directories.

  • /etc/profile.d/*.sh to change things globally (for all users)
  • ~/.bash_profile or ~/.bashrc to change things for a single user

This should work on almost all RHEL'ish derivates and SUSE/openSUSE.

Shell Options (shopts)

Let's be honest: Almost all of us are victims of the default bias, when it comes to default tooling. I see many Linux users rather switching the entire shell, then configuring the default shell to their needs. For some, this makes sense - for others, not so much. So, let's see how we can configure Bash to our liking, without replacing it with Fish or ZSH.

cd without cd

One reason to change to ZSH is cd-without-cd. This means, that we can change into a directory, without using the "cd" command beforehand.

$ cd directory/
# should be the same as
$ directory/

This can be done with a simple shell option. Just enter the below command, and you can start testing it.

# test in the running shell
$ shopt -s autocd

# make persistent (for the current user)
$ echo "shopt -s autocd" >> ~/.bashrc

Spelling issues

If you work a lot on the console, you may type in a wrong directory name, occasionally. The normal output would be something like "no such file or directory". Let's see how we can avoid this.

# default behavior
$ cd Dowloads
bash: cd: Dowloads: No such file or directory

# enable cdspell
$ shopt -s cdspell

# new behavior
$ cd Dowloads
Downloads

More Options

There are way more options, and most of them are in the man pages for bash builtins. You can search for "shopt" after opening the man page via man shopt. You will find much more in this man page, since it redirects you to the "Bash builtins" but, I am sure, you will find the section.

Aliases

Another situation may be, to have some "custom commands" or short handles or (that's how it is officially named) aliases. An alias, is basically a redirection of a command, to another command.

Let's create a very minimal "ToDo App" for the command line with some aliases.

Create the ToDo list

First, we need to create a new list. Since we require only one, this is very easy.

# Regular "create" command
$ touch ~/Documents/todo.md

It's a quite simple command, to create a file, if it is not existing. Typing this is a bit boring and not so intuitive. So, let's make an alias for it.

# Alias for the above command (current session)
$ alias todo_create="touch ~/Documents/todo.md"

# Persist the alias
$ echo 'alias todo_create="touch ~/Documents/todo.md"' >> ~/.bashrc

This will create the new command "todo_create" for us and every time, we run it, the file will be created, if it does not exist already.

Edit the ToDo list

The next thing, we want to do is editing our ToDo list and write down some tasks.

# Regular edit command
$ nano ~/Documents/todo.md

Same procedure as in the last section.

# Alias for the edit command
$ alias todo_edit="nano ~/Documents/todo.md"

# Persist the alias
$ echo 'alias todo_edit="nano ~/Documents/todo.md"' >> ~/.bashrc

And now we can run our brand-new command and edit the ToDo list.

# Run the command
$ todo_edit

Let's put in some content like the one below.

# Keep pushin!

- [ ] write article
- [ ] tell about Bash
- [ ] sync with Tom
- [ ] find pictures
~/Documents/todo.md

Check the ToDo list

Opening the editor for checking our ToDo list seems a bit intrusive. Normally, we just want to have a quick look. So, let's also create an alias for this.

# Regular check command
$ cat ~/Documents/todo.md

# Alias for the check command
$ alias todo_check="cat ~/Documents/todo.md"

# Persist the alias
$ echo 'alias todo_check="cat ~/Documents/todo.md"' >> ~/.bashrc

And now we can run the new alias.

# Run our new alias
$ todo_check 
# Keep pushin!

- [ ] write article
- [ ] tell about Bash
- [ ] sync with Tom
- [ ] find pictures

Checking and removing aliases

By the way, running the alias command without any parameter will provide a list of already defined aliases. You may be surprised what your Linux distribution is already offering.

$ alias
alias todo_check='cat ~/Documents/todo.md'
alias todo_create='touch ~/Documents/todo.md'
alias todo_edit='nano ~/Documents/todo.md'

And, in case you want to edit or remove the above defined aliases, you just need to edit or remove the above lines from your ~/.bashrc.

$ nano ~/.bashrc

More Aliases

Adding aliases to your workflow can be a massive time saver and productivity boost. You can add aliases for all kind of complicated commands. Here is a list of some ideas:

  • gs to shorten git status
  • kgn to run kubectl get namespace
  • ap to run ansible-playbook
  • ll to run ls -l
  • cp should always use cp -i
  • ls should always use ls --color=auto

As you can see, there are many options.

Next Steps

With the next articles, we will also have a look at more configuration options. You can expect something about prompts, openers and better tab completion.

You can find a ton of articles about these two topics in the web.

The Shopt Builtin (Bash Reference Manual)
The Shopt Builtin (Bash Reference Manual)
Aliases (Bash Reference Manual)
Aliases (Bash Reference Manual)
An Introduction to Useful Bash Aliases and Functions | DigitalOcean
The more time you spend on the Linux command line, the more you will begin to realize that you tend to use the same commands, parameters, and sequences repeatedly. Luckily, bash allows you to create aliases and and bash functions to help reduce typing and accomplish complex, repeated tasks quickly …
A Quick Guide on How to Use Bash Alias
This post cover the Bash builtin commands alias and unalias. You will often find Bash alias in a customized .bashrc to improve your shell command-line workflow.
Bash Cheat Sheet Updated for 2022 - from Basics to Shell Builtins
This Bash cheat sheet will show you all useful bash commands that any network or system admin can use as a quick reference.

Conclusion

As you can see, there are some options to customize your Bash. In some future articles, we will have a look at other options. In the meantime, I would be happy to know which aliases and shopts you are using and if you use other customization options.