Matt Kneiser

My "Hello World!" Bash Script

Posted on March 30, 2012 at 3:24 AM

How I got into Bash Scripting

I often pick up new languages or skills when I least expect it. Last Friday I was furiously hacking away at a project for my Algorithms and Data Structures class that was due at midnight. But let's rewind to the prior week - when I had "plenty of time to finish the project."

I was stuck with a part of my project so I changed gears and began writing a program to generate test files called generate.cpp. There was just some simple logic in this program that printed to standard output. I made a Perl script to run generate.cpp many times and redirect the output to new text files. Thus, I was able to run my project against (pseudo-)randomly generated test cases.

Great! So I had a Perl script and a cpp file to test my project with. But, I took it one step further. To submit to the autograder for class I needed to create a tarball, submit.tgz, of all my source code. So I found out that I needed to make a bash script to automate the process of creating new test cases with the Perl script, tarring my submission, and moving things between my organized folders by running one command. After a few hours late at night, I had it! I want to show you the basic things I needed to know to get started. This post should help you get your feet wet. By all means, use stackoverflow and google to continue to learn!

Make your own Hello World! Bash Script

The first line of your Bash Script must contain a shebang. This is a directive that tells the Operating System which interpreter to use. It looks like this:

#!/bin/bash

Note, however, that the hashtag otherwise stands as a line comment. Also useful - use the command "echo" followed by a string to print something to the terminal.

#This next line will print out "Crikey!" to the terminal
echo "Crikey!"

The most useful thing about Bash Scripts is running a system command. No special syntax for this, just put it on its own line.

These scripts have some C behaviors like looping, branching, and the use of variables. Set variables with the assignment operator, no need to be type-specific. Then refer to them with the '$'.

i="0"
echo $i

Incrementing a counter is strange, but here it goes.

i=$[$i + 1]

While loop syntax. Note how comparisons are done with comparison operators.

while[ $i -lt 10 ];
do
#run commands here
done

The branching syntax is very similar to that of looping.

Alas! I have shown you the basic commands I needed to make my first Bash Script. One last thing - I need to show you how to run the program you just wrote. Save your freshly made script with an extension of ".sh". Then, type a "./" followed by the filename in the terminal, and you're off!

Matt@ubuntu:~$ ./HelloWorld.sh

Start Writing Your Own Scripts

When things were down to the wire and I didn't have a lot of time to spare, I could count on running that bash script to save time. It literally shaved minutes off submission time, not to mention stress!

Bash Scripts have many other really neat purposes. What intrigues me the most about them is how they automate tasks. Automation is the single greatest benefit of modern-day computers. Why not exploit this automation?

-Matt