Dotfiles: an overview and application of .bashrc

· tips

So it started as a bug that ended up being a feature. Hidden files are the ones that start with a dot .. Plain simple, in fact if you type in ls in the shell you won’t find any. However in every directory in a UNIX/Linux based system there are 2 files which are . and .., you may know what they mean. . stands for the current directory while .. stands for the previous directory up the tree of directories. So for example:

cd ..

Will take us to the parent directory, except when there is no one anymore (that is when are you at the root directory, i.e. absolute top).

To view those hidden files, just do the following:

ls -a

This will view all files including the hidden ones.

Now why are those . and .. considered as files, well because a file in linux is a loose-term, it is a set of files, directories, symbolic links, devices (like a hard-disk) and other things. In this case those the above are Symbolic Links.

But any file that starts with . is a hidden file. Let’s make a new file named .toast and list all files and see that it is not their but when viewing it with the last command below, it is there.

touch .toast

ls

ls -a

So what are dotfiles, they are basically hidden configuration files. If I want to store a configuration once and for all for a specific program, I would name the file with the required namings and then whenever that program encounters the hidden file, it will read it and do whatever it needs with the commands/configuration.

.bashrc:

In fact .bashrc is basically a Bash script that runs once a Bash terminal enters a directory having it.

Why bother? Lets say you have a team of programmers working on a software and the software is complex. When a new programmer joins, that new person will need to have all the same configuration as their team so no future errors occur due to misconfigurations. One way to do that is just to spend hours reconfiguring and working on the things. Another less frustrating way is to have a common .bashrc file that the entire team uses while programming with the same config and everyone has less difficulty and hassle navigating through the system. That is the idea.

Configurations in .bashrc include custom Bash functions, aliases, environment variables and others. It is run whenever a new terminal session enters that directory. Not only that, an existing shell can still run a .bashrc like a new terminal session like so:

source ./.bashrc

Note other shells do in fact have their own named equivalents. Don’t think it is only for Bash.

Creating a .bashrc:

I will make a new .bashrc for my own project, this project is version controlled using Git:

touch .bashrc

Will edit the file using your favourite editor, I will Nano, it comes with Bash shells.

nano .bashrc

Will create an alias and print to the terminal we’re done with config:

alias gs='git status'

echo Done Bash config

Save.

Now since we’re on the same terminal session, will run the following to read Bash config:

source .bashrc

Sweet. We can run gs command, it is an alias for git status. It should work.

If you are on Windows using a Bash terminal, you may have to use source .bashrc always when entering a new directory as I found it not working all the time.

That’s it.

.bashrc on Mac:

If you are using a Mac, things change. A .bash-profile will do the same work as .bashrc for Bash. Why you might say? The explanation I found is that because on Mac, users are logged in. I didn’t find this helpful. So to make sure your Bash config works both ways, you need to duplicate your config to both .bashrc and .bash-profile.

But wait, you don’t need to really duplicate it. The following YouTube video shows how you can run a Bash if statement to check if .bashrc exists and run the Bash config immediately.

Remarks:

The same way developers customize their Integrated Development Environments (IDEs) others will customize their shells. It is interesting. Not only that, it relieves stress and frustration when considering the fact your configuration is in a file that you could store on Git or any other version control system and share it with others easily.