Advance

What is bashrc and why is it important?

What is bashrc

Bash scripting can be used for many things, including executing the Shell command, running multiple commands together, customizing management tasks, executing tasks automatically, and more. That’s why basic Bash programming knowledge is important for any Linux operating system user. Each Linux skin has a set of files called Startup Files. Because after loading and booting Linux and when starting Linux and login to the system, these files are read. The boot files per user are located under its home directory. This article gives you a brief overview on bashrc.

Introduction to bashrc

In Linux distributions, there are three files in the user’s home directory, all three of which are hidden. These files are called bashrc, bash_profile, and bash_history. The following command shows a list of all hidden files under the home directory whose names begin with the bash string. The following figure shows the output of the command. Of course, Linux distributions also have another file called etc /profile/, which are part of the Bash shell driver files. Of course, in the output below, there is a file called bash_history, which is not a boot file. So the following four files (along with etc /profile/) are a number of bash shell driver files.

ls -ld $HOME/.bash*

$HOME/.bashrc

$HOME/.bash_profile

$HOME/.bash_logout

etc/profiles

Note: The $HOME variable is one of the environmental variables that is created by default after Linux installation and holds the path to the home directory of the current user logged in.

Recommended Article: How to install LEMP stack on Debian 10

This article is about the bashrc file and therefore only explains about this file. The bashrc file is commonly used to identify environmental variables, which are used by some applications such as Java, applications written in Java, and Oracle.

For example, some programs written in Java need to read the Java installation path from a variable called $JAVA_HOME. Also in the link below, we showed how to prepare the Oracle installation environment, which includes building and identifying some variables.

This file also specifies the behavior of the Interactive shell, which is why some suggest that we write our own custom variables in another file, $HOME/.bash_profile.

The bashrc file contains shell script code that, reading and understanding it first helps you become more familiar with shell scripting and also helps you better understand the bash shell.

What is bashrc

bash interprets your typed input in the Terminal program and runs commands based on your input. It allows for some degree of customization using scripting, which is where bashrc comes in.

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.

Terminal preferences can contain a number of different things. Most commonly, the bashrc file contains aliases that the user always wants available. Aliases allow the user to refer to commands by shorter or alternative names, and can be a huge time-saver for those that work in a terminal regularly.

Why should bashrc be edited?

There’s a couple of useful tricks you can do to make your terminal experience more efficient and user-friendly.

1) Functions

# FUNCTIONS #  #######  md() { mkdir -p $1 cd $1 }

In addition to shorthand command names, you can combine multiple commands into a single operation using bash functions. They can get pretty complicated, but they generally follow this syntax:

function_name () {  command_1  command_2  }

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.

md () {  mkdir -p $1  cd $1  }

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

2) Aliases

Aliases can also allow you to access a favored form of a command with a shorthand code.

Let’s take the command ls as an example. 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. As such, 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 “human readable” units instead of blocks.

You’ll need to format your aliases like so:

alias ll="ls -lha"

Type the text you want to replace on the left, and the command on the right between quotes. 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.

3) Bash Prompt

The bash prompt allows you to style up your terminal and have it to show prompts when you run a command. A customized bash prompt can indeed make your work on the terminal more productive and efficient.

How to edit bashrc

You can edit bashrc in any terminal text editor. We will use nano in the following examples. To edit bashrc using nano, invoke the following command in Terminal:

nano ~/.bashrc

If you’ve never edited your bashrc file before, you might find that it’s empty. If not, you can feel free to put your additions on any line. 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:

source ~/.bashrc

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

Note: Edits in bashrc have to follow bash’s scripting format.

Conclusion

The .bashrc file is a good place therefore to run commands that you want to run every single time you open a shell. The .bashrc files is basically a file that is run whenever you open a new shell. The .bashrc file is a very powerful tool and is a great way to customize your Linux shell. Used in the correct way you will increase your productivity tenfold.
In this tutorial, we introduced you to the bashrc file and how to edit it.

We Are Waiting for your valuable comments and you can be sure that it will be answered in the shortest possible time.

9 thoughts on “What is bashrc and why is it important?

    1. The only way to transfer and run C and C ++ programs is to copy the source code, build the program, and run it. These programs may even use special architectures that prevent them from running on other systems with different architectures.

    1. In Bash, you can do this safely and easily with the nullglob and dotglob options (which change globbing behavior) and an array.

    1. You can use the if condition on one or more conditions. The condition starts with if and ends with fi. Create a file called simple_if.sh and paste the following script to recognize the if function in Bash scripting. The definition assigns the condition below 10 to a variable called n. If the value of n$ is less than -1, it declares that this is a 1-digit number. Otherwise the output will be a 2-digit number. -Lt is used for comparison here. You can also use -eq for “equals”, -ne for unequal and -gt for “greater than”.
      #!/bin/bash
      n=10
      if [ $n -lt 10 ];
      then
      echo "It is a one digit number"
      else
      echo "It is a two digit number"
      fi
      Run the file with the following command:
      bash simple_if.sh $

Leave a Reply

Your email address will not be published. Required fields are marked *

We are by your side every step of the way

Think about developing your online business; We will protect it compassionately

We are by your side every step of the way

+8595670151

7 days a week, 24 hours a day