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's 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”
Which Exactly Are Bashrc Records and How to Make Use of 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
/home/gary/.bashrc
/root/.bashrc
/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.
Just what Can You Do With .bashrc Records?
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 colour on poor control
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.
Code:
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"'
Multi-line instantaneous
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:
PS1="\n\[\033[35m\]\$(/bin/date)\n\[\033[32m\]\w\n\[\033[1;31m\]\u@\h: \[\033[1;34m\]\$(/usr/bin/tty | /bin/sed -e 's:/dev/::'):
\[\033[1;36m\]\$(/bin/ls -1 | /usr/bin/wc -l | /bin/sed 's:
::g') files \[\033[1;33m\]\$(/bin/ls -lah | /bin/grep -m 1 total
| /bin/sed 's/total //')b\[\033[0m\] -> \[\033[0m\]"
Display directory info
With this prompt, there are user/host, number of jobs, and date/time on the top line. Below that is the current directory along with the number of files in that directory and their disk usage.
Code:
PS1="\n\[\e[30;1m\]\[\016\]l\[\017\](\[\e[34;1m\]\u@\h\[\e[30;1m\])
-(\[\e[34;1m\]\j\[\e[30;1m\])-(\[\e[34;1m\]\@
\d\[\e[30;1m\])->\[\e[30;1m\]\n\[\016\]m\[\017\]-
(\[\[\e[32;1m\]\w\[\e[30;1m\])-9\[\e[32;1m\[\$(/bin/ls -1 |
/usr/bin/wc -l | /bin/sed 's: ::g') files, \$(/bin/ls -lah |
/bin/grep -m 1 total | /bin/sed 's/total //')b\[\e[30;1m\])--> \[\e[0m\]"
High-performance easy instantaneous
If you want something that packs a lot of info without slowing down your computer, you can use this prompt.
Code:
PS1="[\d | \T -> \w ...\$?]\n#"
The Rob prompts
This is a simple and small code, but it allows the features found in the larger, more complex examples.
Code:
PS1="\[\033[0;33m\][\!]\ \if [[ \$? = "0" ]]; then echo
"\\[\\033[32m\\]"; else echo "\\[\\033[31m\\]"; fi\'[\u.\h: \'
if [[ 'pwed|wc -c|tr -d " "' > 18 ]]; then echo "\\W"; else echo
"\\w"; fi\']\$\[\033[0m\] "; echo -ne "\033]0;'hostname -s':'pwed'\007"
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.
If you are interested to learn and see more about bashrc you can see here.
Top comments (1)
I'm sorry, is this machine translation?
A "casing", to do "surgeries", accept "orders"... Wha?.. And "getting together with apparatus" just made my day. :)
Also, I'm sorry again, calling a few prompts "a full guide to bashrc" is at best misleading.