If you are using Linux operating system or used before, then you have probably also used Terminal, a command line utility in Linux and Unix-like operating system. Linux Terminal comes with Bash which is one of the Linux shell script just like zsh, dash, csh etc.
Bash is the most common and popular shell script preinstalled in Linux operating system. Whenever you input command in the Terminal, Bash runs .bashrc file and interprets and outputs according to command input.
There are .bashrc file in each user's home directory. The .bashrc file is script that is executed whenever a new terminal session is started in interactive mode. The .bashrc file also contains configuration settings, set environment variables, for the particular user sessions.
There is .bashrc file in /etc/skel/
location which is copied to user's home directory whenever new user is created. User's .bashrc file runs everytime whenever user opens bash. There is also .bashrc file in /root
directory which runs on root user opens shell.
How to edit .bashrc file?
To edit .bashrc file, open Terminal and run the bellow command.
nano ~/.bashrc
This will open ~/.bashrc file in Nano editor. You can also use other editor also to change ~/.bashrc
file by drag and drop to editor or with right click and open it.
Whenever you make changes in your bashrc file, changes will be applied to the next Terminal launch. If you immediately want to apply the changes, run the bellow command after you make changes.
source ~/.bashrc
What to change in .bashrc
There are some tricks you can change in bashrc file and can do more things in Terminal.
Aliases
Aliases allows you to input shorthand code of your long command. It is one type of variable that is assign to long command input. So whenever you input alias in the Terminal, it will execute its value command. This is the basic syntax to create alias.
alias alias-name='command-to-execute'
For example, lets check bashrc file and look for some aliases.
# some more ls aliases
alias ll='ls -alF'
alias la='ls -A'
alias l='ls -CF'
So whenever you enters 'll' command, bash will execute command 'ls -alF'.
By this way, you can also create your personal alias also. Suppose you need to open Google chrome directly from the Terminal, then you need to enter google-chrome command. You can set alias to open Google Chrome.
alias google='google-chrome'
You can also set alias to change current working directory with bellow command.
alias html='cd /var/www/html'
You can also set alias to generate notification. The bellow command will create notification of current date and time.
alias time='notify-send --urgency=low "`date "+%Y-%m-%d %H:%M:%S"`"'
Function
You can also execute multiple command by creating function which executes multiple command.
functionName () {
command1
command2
}
Suppose you want to open Google Chrome and Nautilus file manager with single command, then you can create function for that.
google () {
google-chrome
nautilus
}
Then just run bellow command in the Terminal. It will open Google Chrome and File manager.
google
Change font color for Terminal
Default bash configuration stored in the PS1 variable. Just include the color code information to where you want to change color. Bellow are the list to which you can change color.
\h: Hostname
\W: Current working directory
\$: # if user is root otherwise display a $
Here is list of color code.
White = '\e[1;37m'
Black = '\e[0;30m'
Blue = '\e[0;34m'
Light blue = '\e[1;34m'
Green = '\e[0;32m'
Light green = '\e[1;32m'
Cyan = '\e[0;36m'
Light cyan = '\e[1;36m'
Red = '\e[0;31m'
Light red = '\e[1;31m'
Purple = '\e[0;35m'
Light purple = '\e[1;35m'
Brown = '\e[0;33m'
Yellow = '\e[1;33m'
Gray = '\e[0;30m'
Light gray = '\e[0;37m'
Bellow are some examples of how to change the default color:
This will change Yellow color to username and hostname while red color to current directory.
PS1='\e[01;33m\u@\h: \e[0;31m\W\e[0m\$'
Or give more color to particulars.
PS1="\[\e[32m\][\[\e[m\]\[\e[31m\]\u\[\e[m\]\[\e[33m\]@\[\e[m\]\[\e[32m\]\h\[\e[m\]:\[\e[36m\]\w\[\e[m\]\[\e[32m\]]\[\e[m\]\[\e[32;47m\]\\$\[\e[m\]"
Note:
run source ~/.bashrc
command everytime to change effect immediately.
Restore bashrc file
If you have done something wrong and want to edit from start, you can always delete bashrc file and copy from the /etc/skel/ directory.
cp /etc/skel/.bashrc ~/
Conclusion
bashrc is useful to customize your Terminal command and gives you power to work more efficiently. Try out it and give your performance boost.