Bash For Newbies
When I was a student at the New York Code + Design Academy, I had an awesome TA who was helping me debug a project. I suppose he got bored with having me git add, git commit, git push heroku master because he quickly put together some kind of magical shortcut that allowed me to write ‘gach’ in the terminal and voilá: four commands sprinkled with wizardry and turned into four letters.
That’s the power of bash. It can take long, mundane tasks and turn them into a few letters. I wanted to get better at using bash and found a bunch of resources to help with that (they’re listed below). I wanted to create a newbie friendly one stop magic shop to get others started.
Let’s get Started
First thing you’ll want to do is check whether you have a .bashrc file in your home directory:
The bash file is a hidden file so you can use
ls -a to show hidden files. I didn’t have this file so I opened it in my preferred text editor, VSStudio Code using
code .bashrc. If you already have the file you can just open it up, or create one and open that up.
Creating an Alias
In your .bashrc file* you’re going to create an alias or shortcut. I created a shortcut to my coding folder:
alias cpcod='cd ~/Desktop/personal_projects/coding'
Here I have created an alias and have called it cpcod. It’s a string that, once evaluated, cds into my coding folder. You can cd into any folder you want. Remember to use ‘~’ so if you run this command from some random folder it knows the path should start in your home directory. Also notice I have no spaces before or after the ‘=’ as I got a bad assignment error when I tried this with spaces.
But that’s it. My first bash alias. We’ll learn to add functions in just a sec, but for now, you can save the file, go back to your terminal and run:
This will evaluate the file and you can now use your alias.
source ~/.bashrc… no thanks
You probably don’t want to run
source ~/.bashrc every time you open your terminal. Lucky for us, Miqad Amirali laid out the steps for having the source file load when we open our terminal here.
In short, if you’re also using iTerm 2 you just click Profiles >> Open Profiles… and then click the Edit Profiles button and write source ~/.bashrc in the ‘Send text at start’ input field like so:
If you’re using the Terminal just open your preferences. Go to the Profiles tab, click Shell at the top, then click run command and type
Now your terminal will use this source whenever it runs.
We wrote a simple alias above, now we’re going to write a really simple function that will create a skeleton for a project. I want to be able to quickly create a directory that has an HTML, CSS, and JS file along with a ‘public’ folder that also has an ‘images’ folder inside of it. If I was in the terminal I would do the following
touch index.html index.css index.js
mkdir public && cd public
A very simple skeleton of a project that took me about 20 seconds to type. Over time this will probably add up to 2.5 years** so we can make this process easy by opening up our .bashrc file and adding the following function.
Now if I wanted to create a new project I could just write:
The $1 is referring to the first argument we enter after we call the function. If I had written
skeleton NewProject argument2, I would be able to use $2 to refer to argument2.
I then run
source ~/.bashrc and now I can use the skeleton function in my terminal. Easy enough right?
You can get creative and automate a ton of processes. Speed up your git flow, ping websites, your imagination is probably better than mine. Have at it!
*You probably have a .bash_profile. Here’s a great reference for using the bash profile instead of sourcing your bashrc file. From what I have read this would have required moving the path and export into the .bashrc file and I had already set things up to work my way and so I went with that.
**I have no evidence to back this up.
I recently followed this tutorial and am now using iTerm2 on my Mac.
An introduction to Useful Bash Aliases and Functions : https://www.digitalocean.com/community/tutorials/an-introduction-to-useful-bash-aliases-and-functions
How to create your own Custom Terminal Commands: https://medium.com/devnetwork/how-to-create-your-own-custom-terminal-commands-c5008782a78e
.bash_profile vs .bashrc: http://www.joshstaiger.org/archives/2005/07/bash_profile_vs.html
Made a Custom Terminal Command: https://medium.com/@miqdadamirali/made-a-custom-terminal-command-3e1bbfbf2013
A Guide To Building a great .bashrc https://medium.com/@tzhenghao/a-guide-to-building-a-great-bashrc-23c52e466b1c