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 of Linux shells. 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 what BASh is and what .bashrc files are, and how to use them.
What Is a Shell?
A shell can be described as an interpreter that can accept commands from the 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 of shells for Linux as it also allows some degree of customization through 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. Any commands that you could type at the command prompt, You can put in that file. 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 file is copied into the home folder of any new users that are created on a system.
/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.
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 the 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.
PROMPT_COMMAND=‘PS1="[\033[0;33m][!]‘if [[ $? = “0” ]];
then echo “\[\033[32m\]”; else echo “\[\033[31m\]”;
fi’[\u.\h: ‘if [[ \pwd|WC -c|tr -d " "’ > 18 ]]; then echo
“\W”; else echo “\w”; fi’]$[\033[0m] "; echo -ne
“\033]0; hostname -s’:‘pwed’\007”’
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 “ls”;
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 this to create shorter versions of commands, 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 the alias command: $ alias
Now you are shown the default aliases defined for your user in Ubuntu 18.04