This is the first of a series of articles that are designed to ease people into using bash and writing bash scripts. This article will give you a quick overview of some of the commands you will need to use to execute every bash script, and are commands that you should keep close to your heart for the future.
If you haven’t already checked out our basic history and introduction to Bash, it’s a good place to start before undertaking this tutorial.
- The Mac OS X Terminal or a Command Prompt
1. Echoing a phrase
Before we write a script we need to learn the command for printing text to the bash shell. In bash this is a very simple command called echo. To test this out you have to open up the terminal application found at Applications/Terminal.app and type in:
echo "Hello World"
Now we want to greet our friends in the world multiple times and we don’t want to have to type this command each time we say hello. So we are going to write a script.
2. Creating a new script
Creating a bash script is easy. You just have to open up your favorite text editor (or TextEdit on every Mac), and create a new plain text file. If you are using TextEdit go into preferences and under the format options choose plain text. It’s less funky that way. Now you have to click save as (File->Save As) and save the file as hello.sh in your documents folder.
3. Writing the code
Now that we have created our script we can fill in the code we need to actually print out hello world in bash. To start we need one very important piece that all bash scripts start off with, the shebang. This specifies what shell we are going to be executing our script with. Since we are using bash, at the top of every script you write should be:
If you are writing other scripts for other shells you could have:
for the C and Perl shells respectively.
Below this shebang comes the actual meat of our script. For today, the meat isn’t going to be as meaty as it will be later; future articles will explain how to write more detailed and complicated scripts. For now, your second line should read:
echo "Hello World"
Now save your script and get ready to head back into Terminal.
4. Running the Script
To run the script we need to hop back into the terminal application (found at Applications/Terminal.app). Before we can run our pretty little script we need to compile it to make it executable. To do this we need to point Terminal to the folder holding the hello.sh file via a command called
cd. It’s a simple command that goes like:
If you saved the hello.sh file to your “documents” folder you would type:
Next, we need to compile the script. Compiling uses a command called chmod. To compile the hello.sh file in our current directory we type:
chmod +x ./hello.sh
This compiles the script file into a form that can be run by the shell. To actually run the script we merely need to type
You should get a response something along the lines of:
5. Looking to the Future
The next article on bash scripting will go into more depth about writing scripts, and will introduce some concepts like variables and functions. Looking even further forward, we’ll be talking about more advanced functions that are built into bash and the terminal.
If you want to do some experimenting on your own time, there are two great resources you can use to get an understanding about exactly what a function does. One of those resources is always the internet, but there’s also a cool utility built into most Unix systems called the “man pages”, indicating something similar to a “manual”. To use the “man pages”, type:
man "Name of a command"
and a whole plethora of information relating to the command will pop up. To get out of the man page just hit ctrl+z.