One thing that gets debated over and over in Linux is the need for the command line. Many say that you should never need to access a command prompt to get things done, others are of the opinion that the power of the Linux shell is one of the things that makes Linux so great. While I would fall into the latter category, I can certainly understand why people can get frustrated with having to learn complex and seemingly obscure commands to get their computer to work right. Even if you hate the command line, you often have no choice but to open it once in a while to get the job done. Today, we’ll be covering various ways to make it a little easier to navigate.
For those just starting out with Linux, or the command line specifically, the program that displays the command prompt and handles your commands is called bash, or the Bourne Again Shell. In this article we’ll be using the words command line and shell interchangeably to refer to interacting with Linux through bash.
Today we’ll be covering:
- Changing the shell startup routine
- Making a more useful command prompt
- Creating aliases for long or complex commands
.bashrc and .bash_profile
These two files are often the source of some confusion. At first appearance, they seem to do the exact same thing – running startup instructions every time you open a command prompt. In short, .bash_profile runs when you start a shell from a login screen (such as a console system login) and .bashrc runs for non-login shells (such as running Terminal from your system menu). The .bashrc file is the one you’ll be using the majority of the time so that’s the one we’ll use in our examples today.
As I said, .bashrc is loaded by bash whenever you open a new shell. Chances are you already have a .bashrc file that was created for you when you first installed your Linux system. To see what’s already in there, load up your preferred text editor and open
(By default, the file is hidden from public view. You’ll have to right-click and select Show Hidden Files before it can show itself.)
You’ll probably see several lines of code, bash shell scripting to be precise, already in there. The default .bashrc in most distros has some code to do things like color prompts (which we’ll get into a little bit) and some handy aliases (which we’ll get into a lot).
Feel free to glance through the current contents of the file so you have an idea what’s already happening on each new shell session, but for the sake of this article we can safely ignore the current settings and just add what we want to the end.
Instead of putting our new items directly into the .bashrc file, we’re going to take a slightly different approach. Each user on the system has their own .bashrc file in their home directory. If we wanted to make changes to the way bash runs, we’d have to place our changes in each of those files. If you later decide that what you did wasn’t quite what you wanted, you’d have to go through all of those files again and make your updates. Instead of all that hassle, we’re going to make a single file with all our custom settings, and just tell .bashrc to load from that file. The diagram below demonstrates:
By placing the source command at the end of the .bashrc file, it will pull our commands in from the custom-shell.sh file and overwrite the default prompt and default aliases with the ones we’ve specified. This way, we can make a single file (/etc/custom-shell.sh or whatever else you’d like to name it) and just tell each user’s .bashrc file to load it. So if you’ve still got your .bashrc file loaded in your text editor, move all the way down to the bottom and add
Note – you can take this a step further and add the source line to the .bashrc file in /etc/skel. That way, all new users created on the system will already have our source line included!
Save the .bashrc file and then create a new, blank text file named custom-shell.sh. This is where we’ll be putting all of the new settings in the following sections.
Every system has a default prompt. Usually, it’s a bland, default prompt with only a little bit of useful information. It probably looks something like:
To test out exactly how prompt customisation is done, enter the following into the shell:
export PS1="Custom > "
This will turn your prompt into a simple arrow. No useful info of any kind, but it shows the basic idea of how you change the contents of your prompt.
Over the years, people have taken prompt customisation to the extreme, putting all kinds of info into it. I won’t go into a huge amount of detail on how to handle colors and multi-line prompts and adding live info, but I will give a nice looking, useful prompt that you could use as a base for your own. In the custom-shell.sh file we created earlier, paste in:
export PS1="\n\[\033[1;36m\]\u\[\033[1;37m\] \[\033[0;36m\]`date`\n\[\033[0m\][\[\033[1;33m\]\w\[\033[0m\]] "
Most of what you see there are color codes telling bash to switch back and forth between various colors for the text in the prompt. The end result of all this gibberish is a rather nice looking and easy to understand prompt.
Save the custom-shell.sh file after pasting in the code above. If you save it in /etc, you’ll probably need root privileges. You can save it anywhere you want, but make sure you point your .bashrc to the right place. To test out your new prompt, you can either close and re-open your shell, or just type
into your current shell.
Your .bashrc file should read our new prompt info from the custom-shell.sh file and make your prompt nice and fancy with colors and all.
For an EXTREMELY detailed guide on prompt magic, see the Official Linux Prompt HOWTO.
I might as well tell you up front – I love aliases. The first thing I do when I install a new system is set up my preferred aliases. As the name implies, an alias is just another name for a command. For example, my most used alias is one I call “gimme” which bash translates to “sudo apt-get install”. You can use aliases for a variety of reasons, such as shortening a long command or making the Linux shell act more like a Windows/Mac/Solaris/BSD/Whatever shell. I suggest pasting the following aliases into your custom-shell.sh file. Those who use distros other than Debian/Ubuntu style can translate the apt-get aliases into the appropriate yum command, or whatever package utility you prefer, and of course the same is true for aptitude and portage users.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
# #Apt-related aliases # alias gimme="sudo apt-get install" #Install new software alias acs="apt-cache search" #Search available packages alias purge="apt-get --purge remove" #Removes package and its config files alias update="sudo apt-get update" alias debclean="sudo apt-get clean && sudo apt-get autoremove" #Removes some unneeded files # #Misc useful aliases # alias ls="ls --color=auto" #Plain ls with color alias ll="ls -l --color=auto" #Detailed list with color alias config="dpkg-reconfigure" #Re-run the configuration step for a package alias cmi="./configure && make && sudo make install" #Common steps to install package from source alias numfiles="echo $(ls -1 | wc -l)" #Count the number of files in current directory alias dfree="df -h" #See how much hard drive space is free in easy-to-read format alias favdir="cd /directory/you/use/a/lot" #Quickly switch to a directory you often need (music, documents, etc)
Add whatever else you think might be useful, or change the ones I’ve suggested to suit your needs. Instead of gimme and acs, you could use get and got. Some people transitioning from Windows like to make aliases to mimic Windows commands: copy to replace cp, move to replace mv, ipconfig to replace ifconfig, etc.
Your custom-shell.sh should now look something like this:
Once the custom-shell.sh file is saved, and .bashrc is set to point to it, re-open your command prompt and you should have a nice looking, useful prompt with several handy aliases to make things a little easier.
I would love to hear about you favorite aliases! Leave a comment and share some useful command shortcuts you’ve found, or maybe a nice prompt. The command line can be a very powerful tool, and I’d love to know ways you’ve found to make it even better.