Back to blog
Dec 23 2023
Who else is sick of the tedious dev setup that comes with getting a new computer? This post features some actionable content for creating your own script on Mac, that can be leveraged to other platforms featuring Bash.
What is Bash anyway?
Bash is mostly known as the default user shell on most Linux running machines. But what's a shell? A shell is a command line interface that allows users to interact with their machine via commands as opposed to GUI. Although Bash is primarily a command interpreter, it's also a slick programming language that supports variable, control flow and other features similar to the well known programming languages. But where is Bash used?
In my experience working for companies and as a developer at home, Bash is mostly used for system administration tasks. Think of repetitive tasks that may be time consuming to execute one line at a time that can be consolidated into a single file. That's the beauty of bash. It's more light weight than say Python, another language that's great for scripting and is quick to script with.
Ok ok, you understand Bash at a high level now, but you might be asking yourself what does it look like? Well, here's the classical hello world snippet.
#!/bin/bash echo "Hello World"
Great, so now you're thinking, I see "Hello World" and I assume echo outputs that to the screen but what is this statement at the top of the snippet?
#!/bin/bash is shebang. This line indicates to the Unix system how it should run the script. Now that you have had some background on Bash, let's take a look at my dev setup script that I use for getting started on a fresh machine.
#!/bin/bash
echo "Installing homebrew..."
/usr/bin/ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"
echo "Updating homebrew..."
brew update
echo "Running homebrew doctor..."
brew doctor
TAP_LIST="caskroom/cask"
BREW_LIST="brew-cask node nvm yarn bash git wget ack awscli "
CASK_LIST="spectacle visual-studio-code google-chrome sequel-pro iterm2 spotify "
# install taps
for tap in $TAP_LIST;
do
echo "Tapping... $tap"
if [ $(brew tap | grep $tap) ];
then
echo "$tap already installed... continuing..."
else
brew tap $tap
fi
done
for brew in $BREW_LIST;
do
echo "Brewing... $brew"
if [ $(brew list | grep $brew) ];
then
echo "$brew already installed... continuing..."
else
brew install $brew
fi
done
# install casks
for cask in $CASK_LIST;
do
echo "Casking... $cask"
if [ $(brew cask list | grep $cask) ];
then
echo "$cask already installed... continuing..."
else
brew cask install $cask
fi
done
echo "Running doctor for good measure..."
brew doctor
echo "All done!"
What the heck is going on!? Well let's go through it section by section. We first start with shebang, like we always do, so that the interpreter knows how to parse this file. Next I'm installing homebrew, the popular package manager for Mac. After that, you can see I'm using commands that I would normally run in a terminal but in this bash script with the statements above like: brew doctor and brew upgrade. Following the homebrew initialization, I define some variables. I won't go into to much detail here but you can learn more about the syntax of the language here. Finally, I use the combination of brew and brew-cask to iterate over these items in the defined variables do install the software I'd like on my machine. I've added some comments in the snippet below to guide your understanding.
I hope you've found this post helpful so that you too can also create your own dev setup script. This script has definitely saved me time and stress all at the push of a button. Be sure to run chmod on your script with the appropriate executable permissions to actually run the script on your machine. For example if this script was named dev-setup.sh, we would run a command like: chmod u+x dev-setup.sh. This will update the permissions of the file at the user level so that you can then run: bash dev-setup.sh. Good luck with your scripts!