Making The Linux Command Line A Little Friendlier

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

/home/YourUserName/.bashrc

(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

source/etc/custom-shell.sh

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.

The Prompt

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:

josh@roberts:~$

To test out exactly how prompt customisation is done, enter the following into the shell:

exportPS1="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:

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

source .bashrc

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.

Aliases

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.

##Apt-related aliases#aliasgimme="sudo apt-get install"#Install new softwarealiasacs="apt-cache search"#Search available packagesaliaspurge="apt-get --purge remove"#Removes package and its config filesaliasupdate="sudo apt-get update"aliasdebclean="sudo apt-get clean && sudo apt-get autoremove"#Removes some unneeded files##Misc useful aliases#aliasls="ls --color=auto"#Plain ls with coloraliasll="ls -l --color=auto"#Detailed list with coloraliasconfig="dpkg-reconfigure"#Re-run the configuration step for a packagealiascmi="./configure && make && sudo make install"#Common steps to install package from sourcealiasnumfiles="echo $(ls -1 | wc -l)"#Count the number of files in current directoryaliasdfree="df -h"#See how much hard drive space is free in easy-to-read formataliasfavdir="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.

10 comments

The one big advantage with a GUI and the biggest problem with a CLI is that with a GUI you can see what your options are and with a CLI you have to know what they are. If there was some way to divide the screen into a section with the CLI, a section with a list of commands that was filtered by what was being typed on the CLI (a sort of auto-completing search function and a third section that would show the possible options, switches and arguments for each selected command, a newbie could learn while using. The faster a newbie could learn the commands the more the CLI would get used.

bash has a built-in autocompletion feature. If you type the first few letters of a command, say “firef” and hit tab, it’ll fill out the rest.

If you’re not sure exactly what the command is, or more than one command starts with those letters, you can hit tab twice and it’ll show all possible options. To see all commands starting with “gnome-“, you could type “gnome-(tab)(tab)” and it’ll list out all possible options.

And man (command) in most circumstances will show all the possible options to that command.

I don’t know if this behaviour is expected but when I used the alias: alias install=’sudo apt-get install’

I cannot autocomplete anything after I type intall. For example, if I type: inst #this autocompletes to ‘install’ Which is good but now if I go and do this: install abr #nothing happens but if I were to do: sudo apt-get install abr #this autocompletes to ‘abrowser’ which is the expected output. Does anyone know how to fix this?