Run Bash Commands in Separate Tabs
Making your morning easier
If you work with code then probably every morning looks the same: check emails, pull master blah blah blah…
In this post I will give you a simple solution to this problem that can be implemented in 5 minutes using .bash_profile and .bashrc setup files for Bash on Linux. I will use my own django based Bnice project and its Bash aliases / functions as the basis for this exercise.
Create .bash_profile file if it is not in the system
Follow the instructions below to create and edit .bash_profile file where we will put the commands to start new tabs for us.
Open terminal (Ctrl + Alt + T) and:
The last command will open a text editor. Now we are going to add an example code and explain how new tabs are created. We will want to have the following Bash aliases for commands:
- function to activate our project’s virtual environment and navigate to its root folder
- function for standard django’s python manage.py
- alias for rqworker which is used for running background tasks
- alias for database access
We will then combine all those and create yet another Bash function to start each of them in a new terminal tab with an appropriate title. So instead of having to manually open a terminal and type all those commands we will run one Bash function and get the job done for us
What the code above does?
- We have created a bash function bnice() which activates (python / django project) environment and navigates to its root folder.
- The next bash function is mng() which can take command line arguments (we use “$@” to feed them) and runs django’s management commands.
- Following that we have created 4 bash aliases: dba, srv, rqw, yrn for the background tasks.
All of these shortcuts can be used in bash terminal on their own once we add .bash_profile as the source for Bash in the next step.
Aliases are the simplest way of combining a few commands / command line arguments together under a simple name but they do not take any parameters. Functions on the other had provide us with opportunity to run logic and access environment parameters / command line arguments.
Opening new tabs
The way that starting new tabs works is simple. Have a look at the snippet above where we start the web server for the project for example. Below is a snippet with explanation for each bit of the syntax.
Make sure Bash uses all the new commands and aliases
Now we need to source .bash_profile file in .bashrc file, otherwise nothing will work.
Once you have opened .bashrc in edit mode add the line from the snippet below.
Testing if it works
Now save .bashrc, close the editor and all terminal windows. Restart the terminal which now will load .bash_profile. The aliases and functions that you have created should be available as normal bash commands. Try launching all the project workers (obviously in the example I am using my own project – you will have to adapt to your situation and needs) using proj function or use any of the aliases. Isn’t it nice to be able to type 3 letters and get equivalent of python manage.py rqworker high default low long command?
We have barely scratched what can be achieved using Bash but at least you should have an easier time in the morning instead of typing all those commands (hey how long ca you be excited about repeating them? :)) Any questions? Let me know in the comments below.