Before you go, check out these stories!

0
Hackernoon logoWhy You Should Customize Your Bash Terminal And How To Do It by@Bairesdev

Why You Should Customize Your Bash Terminal And How To Do It

Author profile picture

@BairesdevBairesDev

Malcom Ridgers is a tech expert at BairesDev specializing in the software outsourcing industry.

The Linux bash shell is quite the wonder. With it, you can interact with the operating system in very powerful and flexible ways. To do that, you make use of one of many terminal emulators, such as GNOME Terminal, Guake, Terminator, or xterm.

With the bash shell, you run commands like ls, mkdir, chmod, rm, ln, iptables, and make. This is all done from the prompt, which tends to default a minimal bit of information, such as:

USER@HOST: ~$

Where USER is your username and HOST is the hostname of the machine you're working on. It's not much information but at least you know who you are and where you are.

You should know, though, that the bash terminal is quite customizable. And for many a seasoned Linux administrator, such customizations are often considered a must-have.Β 

Let's first answer the question "Why?"

Why customize your bash prompt

There are a very few good reasons you'd want to customize your bash prompt. One of the more important reasons is to ensure the admin knows exactly the machine they are working on. Or maybe you want to make it very clear that you're working with the root user.

Why would you want to do that? When an admin gets busy, really busy, mistakes can be made. You might be able to clearly avoid making even the slightest mistake if you have a red terminal glaring at you to say, "You're using the root user now!" or that "You're working on your database server at this moment!"

And not only is it crucial to always know which user's prompt you're on, being aware of which machine you're working with could prevent you from taking action on the wrong machine, actions that could have devastating effects (such as uninstalling Java on a web server used for Java development).

These might seem like very small changes, but anything you can use to prevent errors is an important step in your daily admin grind.Β 

With that said, let's take a look at how to make that bash prompt serve our needs.

How to customize the bash prompt

There are a few ways to customize the bash prompt, some are permanent and some are temporary. I want to focus only on the permanent method, because the temporary method doesn't really help us, especially when we're trying to ensure we know exactly what we're doing and with what server.

There are two ways to permanently change the bash prompt. The first is on a per-user basis (which is what we'll use). The second method changes the bash prompt globally, but the problem with that method is that it's not nearly as flexible. And given this is Linux, we want to illustrate how flexible the operating system is.

With that said, there's a file in your user's home directory (which is often indicated as ~/) called .bashrc. This file is responsible for housing user-specific aliases and functions. This file is also where you can place bash prompt customizations.

To edit this file, you'll use a text-based editor like nano. So open the file with the command:

nano ~/.bashrc

How bash handles the default prompt will differ depending on what Linux distribution you use. However, one of the universals is that the bash configuration will always begin with:

PS1=

Let's start with the CentOS 8 .bashrc file (because it's fairly simple). When you open that file with nano, you'll see the line:

PS1='\[\033[01;31m\]\u@\h\[\033[00m\]:\[\033[01;34m\]\w\[\033[01;34m\]#\033[00m\] '

Okay, so it's not exactly simple. But we can break it down.

First, let's talk color. When you see the string \e[ it indicates the beginning of a color change. Colors are indicated with numbers:

  • 30m = Black
  • 31m = Red
  • 32m = Green
  • 33m = Brown
  • 34m = Blue
  • 35m = Purple
  • 36m = Cyan
  • 37m = Light gray

And every color has a typeface:

  • 0 = Normal
  • 1 = Bold (bright)
  • 2 = Dim
  • 4 = Underlined

For example, you could set a bold red color with:

\e[1;31m

Or a normal black with:

\e[0;30m

The next important strings are for various elements you can add to the prompt, which are:

  • \d – Date (day/month/date)
  • \h – Hostname (short)
  • \H – Full hostname (domain name)
  • \t – Time (hour:minute:second)
  • \@ – Time, 12-hour AM/PM
  • \A – Time, 24-hour, without seconds
  • \u – Current username
  • \w – Current working directory
  • \W – The basename of the working directory ($HOME is represented by ~)
  • \$ – Specifies whether the user is root (#) or otherwise ($)
  • \\ – Backslash
  • \[ – Anything that follows will be non-displayed characters
  • \] – Close a sequence of non-displayed characters

Now, let's start to put those together. Let's say we want to create a prompt that indicates (in bright red letters) that we're working on a machine that's used for a Java web development company. We'll indicate that with the word "Java" in bright red letters.Β 

Open the .bashrc file (for the user that will be working on the machine) with the command:

nano ~/.bashrc

At the bottom of that file, we're going to add a line that will print out a bash prompt to include username@hostname :JAVA: $: and will indicate the base of the current working directory. For that we'll add the following:

PS1="\u@\h \e[1;31m:JAVA:\e[0;30m\$\W:"

Save and close the file and open a new terminal window. Your prompt should now look like that in Figure 1.

Figure 1

Our new custom bash prompt, indicating we're on the JAVA development machine.

Now, let's customize the CentOS 8 prompt so we know (without a doubt) we're the root user on that machine. For that we'd need to change the root user .bashrc file by first su'ing to the root user with the command:

su

Edit the root user's .bashrc file with the command:

nano /root/.bashrc

At the bottom of that file, we'll create a prompt customization that prints out the username in red and adds a $ *: at the prompt (for extra warning). That line would look like:

PS1="\e[1;31m\u\e[1;30m@\h\W\$ *:"

Save and close the file. Open a new prompt, su to the root user, and you very clearly see you are using root (Figure 2).

Figure 2

Yes, you are at the root user's prompt, so you should be very careful.

Now that you know, without a doubt, you are at the root user's prompt, you will make sure to use caution. Of course, this just scratches the surface as to how you can change your bash prompt on Linux. But this should give you a start on your journey to some serious bash customizations.

Author profile picture

@BairesdevBairesDev

Read my stories

Malcom Ridgers is a tech expert at BairesDev specializing in the software outsourcing industry.

Tags

Join Hacker Noon

Create your free account to unlock your custom reading experience.