cft

Boosting Your Productivity With Aliases and Functions

Leverage your bash profile: practical tips for macOS and Linux users


user

Jovan Hernandez

2 years ago | 5 min read

If you’ve read any of my prior pieces, it’s not news how much I despise using GUIs of any kind. I advocate anyone who wants to improve productivity to lay off GUIs and start utilizing CLI utilities as much as possible.

Most Mac and Ubuntu users understand the power of utilizing tools such as Terminal to perform batch operations in quick and efficient commands, such as move, copy, delete, and transfer.

Those are useful, but what if you want to go to the … next level?

Leveraging Your Bash Profile

In the most simple terms, a user’s .bash_profile is a placeholder for commands that are to be executed when a new Terminal window opens and bash is invoked.

My bash profile greets me with a cute colorful Apple logo and some system info … like a low battery!
My bash profile greets me with a cute colorful Apple logo and some system info … like a low battery!

Since the .bash_profile file is executed upon a new Terminal window launch or session, we can place commands and add files to be executed and read. This is valuable when trying to customize your shell environment for productivity and efficiency.

I’ll go over a few common uses for aliases and also how I incorporate them (as well as functions) into my workflow.

Improving Existing CLI Commands

Working in the CLI over a GUI already has its benefits. I cover a lot of that in one of my previous pieces here. And while CLI commands already improve productivity by reducing repetitive tasks done through a traditional GUI, repetitive tasks done while working in a CLI environment still exist.

For instance, making a directory is almost always followed up by moving into that directory. Or if you’re moving into an existing directory, that’s almost always followed by listing the contents of said directory. Creating a function can eliminate these repetitive tasks.

One of my favorite simple tricks involves combining the history command with grep to improve accuracy and reduce the time needed to scroll through your 5,000 lines of history to find that specific command you wrote several days ago.

Finding all my ssh sessions with a two-character command. Also, those are fake IPs so don’t bother.
Finding all my ssh sessions with a two-character command. Also, those are fake IPs so don’t bother.

Below are some other shortcut aliases I use that cut out annoying two-part combo commands that feel redundant.

# Bash function to change directories and list contents
cd() {
builtin cd $@
ls -lh
}# Bash alias to search through history with speed accuracy
alias hs='history | grep'# Bash alias to create unique date-stamped folders for Vagrant vms
alias vgu18='mkdir ubuntu18_$dt; cd ubuntu18_$dt; vagrant init ubuntu/bionic64; vagrant up; vagrant ssh'

Manipulating Browsers and Tabs

Once you get the hang of aliases and how they can be used to improve your workflow, you’ll eventually run into situations that require a little more logic and engineering.

Creating custom functions and dropping them in your .bash_profile is a great way to gain back some time for shortcuts to everyday commands.

# opens browser pre-filled with tabs
personal_finance(){
open https://www.401k.com
open https://www.finviz.com
open https://www.m1finance.com
open https://www.personalcapital.com
open https://www.robinhood.com
open https://www.seekingalpha.com
open https://www.stocktwits.com
open https://www.webull.com
}

I created a function that opens all of my financial websites I frequent when checking in on markets or news in general. While it may seem trivial, having these processes packaged as a function grants me flexibility and creativity in how I want to call this function.

Let’s Walk Through a Practical Example

Yes, I could just open a browser, type in all these websites, and get the same result. Heck … I could even group all of those sites into a bookmark subfolder and launch all those bookmarks with the click of a button. Standard stuff.

But I’m not talking about standard stuff. I’m talking about running cron jobs to call a function every morning, Monday through Friday at 9:00 a.m., a half hour before the market opens, to launch the websites you need for the day, meanwhile blocking domains you find distracting, like Twitter and Reddit, or useless, like CNN.

Calling the personal_finance function launches a browser with my listed tabs
Calling the personal_finance function launches a browser with my listed tabs

There are other automation tools out there, but with bash functions, cron jobs, and a bit of logic, you can automate your work or dev environment to your liking, even down to the minute of every day — and for free.

You could even customize this even further by specifying specifically what browsers open which sites. I like to separate my browsers for different purposes.

  • Google Chrome is strictly used for work/dev-related items
  • Safari is for side work and freelance gigs
  • The Brave browser is for personal viewing, etc.

By creating functions for each browser and defining tabs within those browsers, I’m saving myself several hours every week by not needing to type in each address in each browser — and all the more, I’m avoiding distractions such as social media and YouTube.

Setting Up Dev Environments

When I was teaching myself how to code and build systems, I struggled not having an environment to test scripts or automate deployments on. I lived in a tiny NYC apartment, so space and real estate were limited for a home network lab. And cloud services, such as AWS and Digital Ocean, were still too scary for me to dabble in (horror stories of huge bills scared me away until I educated myself).

Software and services such as Vagrant and VirtualBox became my go-to tools for testing scripts and automated solutions. I quickly found myself being overwhelmed keeping track of all my Vagrant VMs, though, so I built a function that helps me automate, organize, and deploy servers in a customized date-stamped system.

# Launch Vagrant boxes based on user input
# Folders are date-stamped and numbered accordingly.ubuntufleet(){
n=0
echo "How many machines do you want?"; read n; \
for i in {1..$n..1}; do \
dt=$(date +"%m-%d-%y")
id=$i
FOLDER=$(echo ubuntu18_${dt}_${id}); \
echo $FOLDER; mkdir $FOLDER; cd $FOLDER; vagrant init ubuntu/bionic64; \
cd ../; \
unset FOLDER; echo $id; unset id; \
done
}

Running ubuntufleet in any directory then deploys a number of servers declared by the user.

My ubuntufleet function in action
My ubuntufleet function in action

Conclusion

I want to wrap up by acknowledging this level of customization isn’t for everyone, and it probably looks like overkill. I won’t argue against that if you use your Macbook just to check emails and just browse social media.

But for professional developers or even just professionals in general, repetitive tasks and redundant steps can take up a lot of your time, energy, and motivation.

It’s in your best interest to find workflows that can be streamlined and optimized and to automate them in whichever way possible. I just choose bash aliases and functions because it feels most comfortable, flexible, and granular to me.

Upvote


user
Created by

Jovan Hernandez

Florida man, cloud engineer, trader and investor


people
Post

Upvote

Downvote

Comment

Bookmark

Share


Related Articles