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.


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

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


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.


  1. Mike



    Great 101 article, looking forward to the variables lesson

    • cyberdharma


      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,

  2. zak



    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



    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



    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 to zak Cancel reply