terminal

Bash 101: Hello World (and a little further)

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.

Requirements

    • 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:

#!/bin/bash

If you are writing other scripts for other shells you could have:

#!/bin/csh

or

#!/usr/bin/perl

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:

cd /path/to/file

If you saved the hello.sh file to your “documents” folder you would type:

cd /Users/Your_user_name/Documents

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

./hello.sh

You should get a response something along the lines of:

hello world

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.

5 Comments

  1. Mike

    06.13.2011

    Reply

    Great 101 article, looking forward to the variables lesson

    • cyberdharma

      04.14.2012

      Hello everyone :-)

      I have followed all the instructions above but I continue to get the following message:

      Redacted:redacted redacted$ ./helloworld.sh
      -bash: ./helloworld.sh: /bin/bash: bad interpreter: Operation not permitted

      Does anyone know what bad interpreter means and how to resolve it?

      Many Thanks,
      Cyberdharma

  2. zak

    04.23.2012

    Reply

    hey I really like the article since I am trying to teach myself through what your posts. I am getting stuck though. I don’t exactly understand the format of the writing part. Is there a screenshot you could show?

  3. Joey

    05.05.2012

    Reply


    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

    I thought chmod just changed the file permissions, is that really compiling?

  4. BloodRumpus

    09.11.2012

    Reply

    chmod changes file permissions so that you can run the script. You don’t compile scripts, they are interpreted. You should read the wikipedia article on the subject: http://en.wikipedia.org/wiki/Interpreted_language

Leave a Reply