What is Linux bashrc and how to use it (Full guide)

Image result for linux bashrc wallpaper

If you are a Linux user and are learning about its command line, you might know by now that BASh is a Linux shell and stands for “Bourne Again Shell”. You are also likely to have BASh installed as your default terminal. This is because it is both the most common and, likely, the most popular one of the shells. What it does is that it basically interprets your typed input in the Terminal program and runs commands based on your input. Unlike some other terminal customization tricks, playing around with .bashrc is fairly straight-forward and low risk. If you mess anything up, you can always delete the .bashrc file completely and start over again. In this article we will explore further about what BASh and .bashrc files are and how to use them.

What is a shell?

A shell can be described as an interpreter which can accept commands for a user and run them to perform operations such as navigating around a file system, running programs and interacting with devices. There are a number of different shells including csh, zsh, dash and korn. As was mentioned, BASh is the most common form of shells for Linux as it also allows for some degree of customization using scripting, which leads us to “.bashrc”

What are Bashrc files and how to use them?

.bashrc is a shell script that Bash runs whenever it is started interactively. It initializes an interactive shell session. You can put any command in that file that you could type at the command prompt. It is executed whenever a new terminal session is started in interactive mode. This is what happens when you open a new terminal window by pressing Ctrl+Alt+T, or just open a new terminal tab. In order to load your preferences, bash runs the contents of the bashrc file at each launch. This shell script is found in each user’s home directory. It’s used to save and load your terminal preferences and environmental variables.

So the first step is to open a new terminal window (Ctrl+Alt+T).

The computer returns three results upon running this command:

  • /etc/skel/.bashrc
  • /home/gary/.bashrc
  • /root/.bashrc

The /etc/skel/.bashrc file is copied into the home folder of any new users that are created on a system.

The /home/ali/.bashrc is the file used whenever the user Ali opens a shell and the root file is used whenever root opens a shell.

What can you do with .bashrc files

There’s a couple of useful hacks that you can use to make your terminal experience more efficient and user-friendly. We will explore them here.

  • Bash Prompts

A customized bash prompt makes your work on the terminal more productive and efficient as it allows you to personalize your terminal and have it to show prompts when you run a command. For example you can:

Change color on bad command

This prompt changes color if your last command failed to run successfully, but it also shortens long paths and contains the bash history number of each command for easy retrieval. This can be very helpful and efficient.

Code:

Multiline prompt
bashprompts-informant

This is a multi-line prompt containing date/time, full path, user and host, active terminal, even file count and space usage. This can be useful for those who would like a lot of information in their prompt.

Code:

Display directory information
bashprompts-7

With this prompt, there are user/host, number of jobs, and date/time on the top line. Below that is current directory along with number of files in that directory and their disk usage.

Code:

High performance simple prompt
morebashprompts-minimal

If you want something that packs a lot of info without slowing down your computer, you can use this prompt.

Code:

The Rob prompt
morebashprompts-robs

This is a simple and small code, but it allows the features found in the larger, more complex examples.

Code:

Note – to use any of these prompts, you can copy & paste the “PS1=” line directly into your terminal. To make the change permanent, paste the line to the end of your ~/.bashrc file.

  • Aliases

You can save yourself some time by creating aliases for your most used commands. Aliases are like custom shortcuts used to represent a command (or set of commands) executed with or without custom options. For example, the command “Is”;

By default, “ls” displays the contents of your directory. That’s useful, but it’s often more useful to know more about the directory, or know the hidden contents of the directory. Therefore, we use an alias here. A common alias is “ll”, which is set to run “ls –lha” or something similar. That will display the most details about files, revealing hidden files and showing file sizes in units readable to us instead of blocks.

You can use to this to create shorter versions of command, guard against common typos, or force a command to always run with your favored flags. You can also circumvent annoying or easy-to-forget syntax with your own preferred shorthand.

You can see a list of defined aliases on your profile by simply executing alias command:

Now you are shown the default aliases defined for your user in Ubuntu 18.04

As you can see, $ ll Is equivalent to running $ ls -alF

You can also create your own temporary aliases:

Type the word alias then use the name you wish to use to execute a command followed by “=” sign and quote the command you wish to alias.

The syntax is as follows:

For example:

You can then use “wr” shortcut to go to the webroot directory.

However, the problem with that alias is that it will only be available for your current terminal session. If you open new terminal session, the alias will no longer be available. If you wish to save your aliases across sessions you will need a permanent alias:

To keep aliases between sessions, you can save them in your user’s bashrc file.

~/.bashrc

The syntax you should use is practically the same as creating a temporary alias, except this time you also have to save it in a file. So:

Step 1: for example, in bash, open .bashrc file like this:

Step 2: Find a place in the file, where you want to keep the aliases. For example, you can add them in the end of the file.

Step 3: Save the file. The file will be automatically loaded in your next session.

Note: To remove an alias use the “unalias” command.

  • Editing .bashrc files

You can edit bashrc to add your own commands in any terminal text editor. We will use nano in the following examples.

Step 1: To edit bashrc using nano, put the following command in Terminal:

Note: If it is the first time you are editing your .bashrc file, you might find that it’s empty. It is no problem if that is the case. If not, you can feel free to put your additions on any line.

Bear in mind that any changes you make to .bashrc will be applied next time you launch terminal. If you want to apply them immediately, run the command below:

Step 2: You can add to your .bashrc where ever you like, but use command (proceeded by #) to organize your code.

Note: Bear in mind that edits in .bashrc have to follow bash’s scripting format. If you don’t know how to script with bash, there are a number of resources you can use online.

  • Functions
terminal-edit-bashrc-2

Apart from single short hand commands, you can also combine multiple commands into a single operation using BASh functions. Sometimes they get complicated and confusing, but they generally follow this syntax:

The command below combines “mkdir” and “cd”. Typing “md folder_name” creates a directory named “folder_name” in your working directory and navigates into it immediately.

The “$1” you see in the function represents the first argument, which is the text you type immediately after the function name.

In conclusion, the .bashrc file is a very powerful tool and is a great way to customize your Linux shell. If you get the hang of it and are able to use it in the correct way you will increase your productivity and efficiency by lots!

3
3
0 0 vote
Article Rating
Subscribe
Notify of
1 Comment
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
test
15 days ago
1
0
Would love your thoughts, please comment.x
()
x