Skip to main content

Don’t Be A Robot. Learn Shell Scripting.

11-04-14 Patrick Simpson

Wasting time on mundane, repetitive tasks is for the robots. Release yourself from the monotony. Learn shell scripting and automate your command-line tasks.

Being a developer often requires that you dip into the command line. Whether you are setting up configuration files, starting/stopping servers, or running build tools, you’ll find you repeat yourself, time and time again. Shell scripting is a way to automate command-line tasks, saving you time to pet your favorite cat or play ping pong. To automate repetitive tasks, it’s essential to learn basic shell scripting.

Recently, I installed Nginx on top of Apache to serve up local web servers spun up by some of our build tools. Nginx now sits on top of port 80 and acts as a proxy to various other servers, including Apache (running on another port). This requires adding configuration settings in multiple locations. This repetitive task drove me to write this shell script.

Writing the shell script reminded me how useful they can be, and I learned some new tricks along the way. I wanted to share the basics to get started with shell scripting and tools you can use to make a powerful task automator.

Getting Started

Starting off, you’ll want to include a shebang (#!), followed by the path to the interpreter, like bash (/bin/bash).


The shebang plus interpreter instructs your script to be run with the specified shell, /bin/bash in this case. Once you’re set up, you’re ready to get automating. There are lots of common commands you’ll want to utilize.

Printing messages

echo "Luke, I am your father..."

Print without a newline:

printf "Enter your name:"


Variables must be declared without a dollar sign.

Declaring variables, no dollar sign:


After the variable is declared, you extract the value from the variable by preceding the variable with a dollar sign.

echo $FOO
echo $MYDIR


  • Variables are case sensitive.

  • Prefix your variables, and be careful not to name your variables names like PATH. This will overwrite the system variable PATH for your script, and you may see errors. You’ll want to prefix your variables with something unique (e.g. APP_, or FOO_) to prevent this.

  • Avoid using “export” in your scripts. This can cause harmful system-wide changes.

Accepting Input

printf "Enter your name:"

read name

echo $name

If/Else Statement

if [ $foo = $bar ]; then

echo "Foo is equal to bar!"


echo "Foo is not bar!"



  • Shell if ends with fi (backwards spelling of if).

  • If you put “then” on the same line as an “if,” then you need to put a ; (semi-colon) after the condition.

  • Use  if [ -e $FILE ] to test if a FILE exists

  • Use  if [ -d $FILE ] to test if a FILE exists and is a directory

  • Use  if [ -h $FILE ] to test if a FILE exists and is a symbolic link

  • More on if statements


I find it helpful to separate configurations into other files.

source "inc/config.conf"

This will include the shell script or config file, etc., as if it were written at that point in your shell script.


Functions provide a way to group commands into a simple package of repetitiveness. Just like in any programming language, you can create shell script functions. One rule about functions is they must be declared (written) before they are used.

Shell functions are written as:

function func_name() {

 #code block


The keyword, function, is optional.  In the code block, you can put in any shell commands.

Arguments in functions are not declared in the function definition. You can simply pass arguments in as need be. Each argument passed in will be defined numerically as $1, $2, etc., within the function scope.

func_name() {

 echo $1

 echo $2


func_name "Hello" "World"

This will work and print “Hello World”.


Shell scripting gives you the ability to automate commands and repetitive tasks. Armed with the knowledge of shell scripting, you’ll be hunting down repetitive tasks in your life and scripting them out of existence. Learning to script your shell is the way to make it happen—don’t be afraid to give it a shot. There’s a lot more information that we could cover here, but here are some decent tutorials you can follow up on:

Related Content

User-Centered Thinking: 7 Things to Consider and a Free Guide

Want the benefits of UX but not sure where to start? Grab our guide to evaluate your needs, earn buy-in, and get hiring tips.

More Details

See Everything In

Want to talk about how we can work together?

Katie can help

A portrait of Vice President of Business Development, Katie Jennings.

Katie Jennings

Vice President of Business Development