On my network, I have 2 Raspberry Pi's and one Orange Pi, which I all manage headless. When i'm managing my devices via SSH you can imagine typing "ssh pi@192.168.1.15*" for all my devices separately over and over every time I need to modify something, or log in to take down one of the servers when setting up a new service, gets really annoying. Over time i've modified the .bashrc file on all my computers to speed up this process. I'll show you how here.
What is .bashrc?
The .bashrc file is a shell script that is usually sourced automatically from .bash_profile when logging into a shell. Essentially what this means is, every time you open a new Terminal on your OSX or Linux machine, it will automatically execute all commands in the .bashrc file. This can be especially useful to customize your shell to streamline your flow.
You can locate your .bashrc file in your user home directory.
cd ~
From here you can use whichever text editor you prefer, I prefer nano.
nano .bashrc
And you'll get something like this.
Here's an example of my .bashrc file on my Chromebook. There are endless possibilities in regards to how you can customize your .bashrc file, but i'll just be sticking to one command here today.
The Alias Command
As you see near the bottom of this screenshot, I have 3 lines that start with 'alias'. This command will substitute one command with another command, in my example I have it set up like this.
alias RPI1="ssh pi@192.168.1.150"
alias RPI2="ssh pi@192.168.1.152"
alias OPI1="ssh orangepi@192.168.1.151"
When I type RPI1, bash interprets it as 'ssh pi@192.168.1.150', and will automatically prompt me for the password to login to my Raspberry Pi. Now instead of remembering 3 IP addresses, and the devices they are associated with, I can simply type the name of the device I want to connect to and easily connect.
Something else that you can do with .bashrc that is very useful, and can really boost your productivity is adding a PS1 prompt variable.
PS1 Prompt Variable
The PS1 prompt variable effects how you interact with the shell. For example the default prompt is something like this:
chronos@chromebook / $
Nothing special here, but i've modified it a bit to something like this
chronos@chromebook:[~]:
As you can see I haven't changed much, but you'll notice that it's colored coded now, and the path is blocked in by [] for ease of telling what directory i'm in. I'm not going to go into all the tags to customize your shell, but there is an awesome website - http://bashrcgenerator.com/ that will allow you to drag and drop the elements you want your prompt to take on, and it will give you an easy to copy and paste PS1 string to paste into your .bashrc.
After getting your PS1 configuration from bashrcgenerator, you can copy and paste it into your .bashrc file and save it. Make sure it's all on one line or it's going to look wonky. Either close and reopen your shell, or type:
source .bashrc
You will immediately see the results following this command.
Any interesting things that you include in .bashrc? Comment below!