
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Customize Linux Shell Environment
The shell is the interface between the user and the system. It can be used to do anything you want on your system. In Linux, there are several types of shells that we can use and interact with. In this article, we will explain how to use and customize the shell environment a little bit and introduce some ideas of what you can do to make your shell environment more custom, depending on your needs.
If you are new to Linux, you probably only know the bash shell, but it's not the only one we can use. There are other alternatives and other shells that you may prefer over the standard bash shell.
Sometimes, when running a new machine, the default shell you see may not be bash. You need to know how to switch back to the bash shell if needed. Here, you'll learn how to switch between shell environments and make them work for you.
In this article, we will try to cover all the basics you need to be more familiar with the shell, especially the bash shell, so you can begin exploring other shell environments and their features.
Default Shell
On a Unix system, you should find the shells available to use listed in a file called /etc/shells. Let's view this file and see what shells are available on our system ?
Whenever we log into the system, we see the default shell, but we can change this!
The binary for your shell is usually located in the /bin directory.
One thing you might want to know when working at the command line is which shell you're running. A way to do this is to check the shell environment variable using the echo command followed by a dollar sign and SHELL in all capital letters. This will tell you the shell you're currently using.
In my case, I use bash, which will likely be yours as well if you haven't changed shells.
We can check the default shell we're using by looking at the /etc/passwd file with the grep command like this:
grep mead /etc/passwd
The last item on this line indicates the default shell for this user. In this case, it's the /bin/bash shell environment.
As mentioned earlier, the shell binaries are located under /bin/, and we can view the installed shells on our system by listing this directory. Since all shells end with sh, we can list them like this.
ls /bin/*sh
To switch to another shell, type its name (e.g., sh) ?
Here you can see that even after changing to sh, the default shell is still shown as bash. This is because echo $SHELL shows the default login shell, not the current shell. To see the current shell, use this trick to show the running process.
ps -p $$
The $$ shows the process ID of the current process. If you're in a shell and don't know which one, this trick will help.
Change the Default Shell
The last example only changes the shell for the session. Once you quit and reopen the terminal, the default shell (bash, in our case) will return.
If you want to permanently change the shell environment, use the chsh command, which stands for "change shell." Enter it with the username, followed by -s, and then the shell path you want as the default.
chsh mead -s /bin/sh
This will prompt for a password since you're making a change to the user shell. Enter the password and press Enter.
Now, if we check the shell for the user in /etc/passwd, we should see sh instead of bash as the login shell.
To change it back, use the same command.
Bash Configuration
Let's talk about how we can customize our bash shell environment a bit. In your home directory, you'll find several files prefixed with a dot (.). These files are not visible unless you use ls -a to list all files in that directory. They usually contain configuration for various Linux applications; here, we're interested in .bashrc and .profile, which contain bash shell configurations.
Let's look at what these files do and how we can customize them.
Alias
One useful feature in Linux is the concept of aliases, which allow you to create custom shortcuts for commands you use frequently. Instead of typing a long command each time, you can define an alias in the .bashrc file, like
alias up="sudo apt update"
In this example, we replace the command sudo apt update with up, so whenever I type up, the system will execute sudo apt update.
Prompt
One thing many users want to change is the prompt that appears in a new terminal session. This prompt usually displays the username and, when you navigate, the current location.
We can customize the prompt by modifying an environment variable called PS1 in the .bashrc file.
For example, changing PS1 to ?
PS1="\u\$ "
will display the username followed by a dollar sign.
This is just an example; you can adjust it to fit your needs.
The .bashrc file contains many session configurations. We've only made a few changes to aliases and the prompt, but there are other options you can explore.
Conclusion
This article provided a quick introduction to shell customization and how we can make our shell unique using basic configurations in the .bashrc file. We also covered how to switch and get the current shell environment.