Wednesday, October 7, 2015

Automation: Script to set up tabs for you in a single terminal and run commands in them

I noticed many colleagues loved opening up multiple tabs in a single bash terminal, setting title names for every tab and running commands in them. They performed this ritual manually every single day.

The first thought that went through my mind was "Hey, why don't we automate the process?". I suggested it to them, but I guess they preferred doing it the old way. So when I had some free time, I did a bit of finding out and created this shell script:

#The ampersand in the end of this file makes sure that the gnome-terminal command is run as a background process. 

echo "Setting up environment"; 

--tab-with-profile=Default --title=TAB1 -e "bash -c \"trap 'bash' 2; clear; ls; exec bash\"" 
--tab-with-profile=Default --title=ANOTHERTAB -e "bash -c \"trap 'bash' 2; clear; ls; pwd; exec bash\"" \

Place this code in an empty file named and use the command 
chmod +x 
to make it an executable file and then run it with

and viola! You have a terminal with multiple tabs! Just replace the "clear;ls" and "clear;ls;pwd" with any other commands of your choice.

The syntax for adding more tabs is:

--tab-with-profile=Default --title=<THE TITLE YOU WANT TO GIVE THE TAB> -e "bash -c \"trap 'bash' 2; <THE BASH COMMANDS YOU WANT TO EXECUTE> ;exec bash\"" \ 

One more thing to do

Having the script is not enough. You'll notice that when you finish running any commands on the terminal, the tab title/name you set, would have gotten reset.
To circumvent this problem, do this:

Go to the Edit menu of the terminal. In Profiles select Default. For the option of When terminal commands set their own titles, select Keep initial title

That's it! Don't stop at this. Keep finding ways to automate tasks and save time!

No comments: