Search The Hostwinds Guides Knowledge Base

Customizing the Bash Environment

Share This Article [TheChamp-Sharing]

Bash Environment Customization

Being able to customize the Bash environment on your server has a multitude of benefits such as adding aliases for common commands you use, customizing the prompt displayed when connected, as well as displaying information on login. This article will assist you in learning how to do this.


.bashrc File

Any Bash environment customizations are usually placed in the .bashrc file. It is always located in the home directory for the user and any customizations explained in this article will need to be placed in this file. One thing to note is that the commands placed into the .bashrc file are recognized as bash commands and you can test them in your shell environment before adding them to the file to ensure the customization will do what you want it to.


Set Environment Variables

Bash has some environment variables set to allow for tab-completion of commands as well as to allow you easy access to information about the shell. One of the most common environment variable changes is modifying the PATH variable that tells bash what paths to search for commands. To set environment variables you just need to write out the variable, as shown here.


You’ll notice that there is PATH twice there. To keep the current information in the PATH variable when modifying it, you have to invoke it when setting it and append your changes to the end. You can set any environment variables you know you’ll need here.


Bash Command Aliases

Bash has a built-in system that allows you to add command aliases. Bash command aliases allow you to create your own commands or use certain flags for commands on standard command invocation. For example, one of the most common aliases is for the command ls to list all files, including hidden ones. This example is below:

alias ls='ls -a'

You can also create aliases to perform long commands, such as connecting to another server as shown here:

alias servername='ssh'

Bash aliases are extremely powerful and can increase your overall productivity in the Bash environment. 


Customizing the Bash Prompt

In addition to aliases, you can also customize the way your bash prompt looks. We are going to go over basic customization, but if you would like to read more about controlling the prompt, the official Bash documentation has you covered and that link can be found here. Moving forward, below is an example of a prompt that displays the logged in user, the short hostname, the exit code of the last command passed, and on a new line provides the typing prompt:

Displaying Customized Bash Prompt

To obtain this prompt, you would place the following in the .bashrc file

PS1="[\u@\h:\w] \$?\n\\$ "
  • \u – The current username
  • \h – The short hostname (hostname up to the first ‘.’)
  • \w – The working directory, with $HOME abbreviated with a tilde (i.e. /home/hwtest/txt/ turns in to ~/txt/)
  • \$? – The previous command’s exit code. This will update after each command is run.
  • \n – Newline character
  • \\$ – This displays a $ as the beginning of the command line unless the UID is 0, in which it shows a # as shown above.

There are some powerful things you can do with the prompt customization, and this is just scratching the surface. Generating a prompt that’s not only readable but also informative is key to working in the Bash environment.


Information Display on Login

Since the .bashrc file is executed every time a new bash session is opened, you may find yourself wanting a common command to display information to run as soon as the shell is opened. Not only does Bash support this, but it’s extremely easy to do. I’ve included a sample .bashrc file below as an example.

# .bashrc

# Source global definitions
if [ -f /etc/bashrc ]; then
        . /etc/bashrc

# Uncomment the following line if you don't like systemctl's auto-paging feature:

# User specific aliases and functions

# Environment Variables

# Aliases
alias ls='ls -a'
alias ll='ls -lah --color=auto'

# Prompt
PS1="[\u@\h:\w] \$?\n\\$ "

# Display on session start
df -h

You can see the result of adding the df -hand uptime commands in the following example.

Image displaying an example of adding commands to bashrc

The commands will run every time a new shell session is started so be sure to only put quick and low resource cost commands here or it could delay the session from starting.


Helpful Related Articles