The bashrc file

In this post we made aliases… now we want to save them in a file; bash has such a file called the .bashrc file.  It’s also known as the bash configuration file.

That period before the name is very important, without it it isn’t the right file.  It also means that the filename is hidden from view when you do a ls.  You have to use the ls -a flag in order to see it.

But this post isn’t about ls, it’s about the .bashrc file.  We’re going to use nano, that useful editor we talked about in the last post, to edit the file.

I’ll start with opening the file in nano:

Editing .bashrc with nano

Editing .bashrc with nano

I did this with the command:

Alpha:~ computerlamp$ nano .bashrc

Now I’ll type those two alias commands that I used before.  I aliased more to less and history to whatIdid:

Adding Aliases to the .bashrc file

Adding Aliases to the .bashrc file

And now we save the file:

Saving .bashrc with nano

Saving .bashrc with nano

Well, so now what?  I edited the file, I added those two lines, and saved it… just don’t forget to exit nano when you’re done with control-X!

Once we created the file, we need to use it.  Otherwise, we just saved our aliases in a file to do nothing.

It’s called sourcing  the file.  The command to do it is:

Alpha:~ computerlamp$ . .bashrc

See that dot before the .bashrc?  Be sure to type it.  That’s how bash knows to use its configuration file.  Also, every time you open up a new terminal, you’ll have those aliases.  And you’ll be able to see them with the alias command.

You can also look at what you’ve got aliased by using:

Alpha:~ computerlamp$ cat .bashrc

The output is exactly the same, no matter which one you use.  It looks like:

alias less='more'
alias whatIdid='history'

The .bashrc has other uses aside from aliases, we’ll talk about more in the future.

Leave a Reply

Your email address will not be published. Required fields are marked *