Bash Aliases and Functions: a Programmer’s Productivity Hack

Madison Gipson
Jun 4, 2020 · 4 min read

As a college student, intern, and relatively new programmer, I’m always looking to my professors, mentors and experienced teammates for coding & workflow tips. Recently, I gained a new appreciation for Bash aliases & functions, which has helped me streamline workflows and cut down on headaches.

Photo by Rich Tervet on Unsplash

What is Bash & .bashrc?

Speaking of shell scripts… that’s what the .bashrcfile is! It’s a special one that Bash runs whenever it’s started interactively (e.g. when you open a terminal window). Because it’s a shell script and Bash can read and execute any command included in the file, you can set up variables, aliases and functions in .bashrc and execute them in the command line.

Why should you care?

This isn’t just for programmers; we may get a tad more use out of it if we have a lot of business in the command line, but automating processes and quickly opening applications/folders/files is for everyone.

Plus, it makes you feel super hacker-esque.

Setting up .bashrc

Side note: a lot of people prefer Zsh over Bash, and you can do the same shortcut stuff in both, it just uses different formatting. I personally use Bash because it’s standard across the different systems I use.

To start, find or create your .bashrc file. I’m on Mac and didn’t have one to begin with, so I created one in the /Users/your-username directory and added these simple aliases at the top, for starters:

alias reload = 'source ~/.bashrc'
alias edbash = 'vi ~/.bashrc'

Close the file and return to the /Users/your-username directory. Execute source ~/.bashrc in your command line to reload the .bashrc file. Now executing one of the aliases in the .bashrc file, like edbash, should work.

There’s an issue here though (if you’re on Mac); if you close and re-open the terminal session then try using one of the aliases, it gives the error zsh command not found. Since Mac uses Zsh, you need Zsh to reload .bashrc in each terminal session in order for it to recognize the aliases from that file.

To resolve this, create/edit the file .zshrc from your /Users/your-username directory and add:

[[ -s ~/.bashrc ]] && source ~/.bashrc

Close the file and return to the /Users/your-username directory once again. Try executing an alias from your .bashrc file now; it should work!

You’re well on your way to making Bash your best friend.

Now for the Fun Stuff…

Full disclosure: some of these aliases and functions were provided or inspired by online resources & friends (Lannie Hough). I came up with a few from scratch, but most of my effort was spent coming up with creative ways to apply basic operations.

Everyday Linux Operations

#Lazy way to list files
alias l = 'ls -la'
#Jump to often-used directories
alias sr = 'cd ~/Documents/School/Senior\ Research'
#Change directory & list files
cl() {
cd $1
ls -la
}
#Make directory & enter it
mkcd() {
mkdir -p $1
cd $1
}
#Search for a specific file
#Use: "findfile example"
#Results: prints any files that begin with "example", is not case-sensitive, picks up any file type (ex. result: ExampleTest.docx)
findfile() {
file = "$@"
file += "*"
find . -iname $file 2>&1 | grep -v "Operation not permitted"
}
#Search for all files with a specific extension
#Use: "fondest swift"
#Results: prints all .swift files
findext() {
ext = "*."
ext += "$@"
find . -iname $ext 2>&1 | grep -v "Operation not permitted"
}

Git & CocoaPods Handling

#Git
alias pull = 'git pull origin master'
alias add = 'git add --all'
alias push = 'git push --all'
gitcombo() {
add
message=""
for arg in "$@"
do
message += "$arg"
message += " "
done
git commit -m "$message"
push
}
#CocoaPods
pod refresh() {
pod deintegrate
pod update
pod install
}

Process Control

stopcoding() {
pkill Trello
pkill Xcode #seems to save changes (I tested it)
pkill Simulator
pkill Discord
pkill SF\ Symbols
}
freeram() {
pkill Mail
pkill Calendar
pkill Music
pkill Notes
pkill Pages
pkill Messages
pkill -9 Microsoft\ Outlook #complained without the -9
#pkill literally any application... you get the point
#but I don't include Safari/Chrome or apps used constantly
}

Project Setup

#Project Shortcuts (commands generalized)
alias projectgit = 'open https://github.com/mgipson/MyProject'
alias project = 'cd ~/Documents/ProjectFolder; open -a Xcode MyProject.xcworkspace'
project() {
projectgit
project
open -a Trello
}

Happy hacking!

Dev Genius

Coding, Tutorials, News, UX, UI and much more related to development

Sign up for Best Stories

By Dev Genius

The best stories sent monthly to your email. Take a look.

By signing up, you will create a Medium account if you don’t already have one. Review our Privacy Policy for more information about our privacy practices.

Check your inbox
Medium sent you an email at to complete your subscription.

Madison Gipson

Written by

Student & intern learning how to make the world a better place with code.

Dev Genius

Coding, Tutorials, News, UX, UI and much more related to development

Madison Gipson

Written by

Student & intern learning how to make the world a better place with code.

Dev Genius

Coding, Tutorials, News, UX, UI and much more related to development

Medium is an open platform where 170 million readers come to find insightful and dynamic thinking. Here, expert and undiscovered voices alike dive into the heart of any topic and bring new ideas to the surface. Learn more

Follow the writers, publications, and topics that matter to you, and you’ll see them on your homepage and in your inbox. Explore

If you have a story to tell, knowledge to share, or a perspective to offer — welcome home. It’s easy and free to post your thinking on any topic. Write on Medium

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store