1. Home
  2. Bash
  3. Bash Tutorial
  4. Bash – Hello World

Bash – Hello World

Hello World in Bash

Bash (Bourne Again Shell) is a command language interpreter for the written Unix-like systems written by Brian Fox for the GNU Project. It is a free software application that replaces the Bourne Shell (sh) interpreter.

Before starting with your first script, you must know about shebang’s character.

What is Shebang (#!)

A Shebang is declared at the very first line of any shell script start with the “#!” symbols and followed by the shell interpreter. This tells the system which interpreter is required to run this script only if not defined at the command line during runtime.

#!/bin/bash
...

The above method is correct and works properly. Here is another way to define shebang characters with the environment. This is helpful to create cross environment scripts.

#!/usr/bin/env bash
...

Here the script will automatically find the bash location instead of defining a fixed location in first example.

To read more about shebang visit here.

Write Hello World Program

Now create a new file and edit in text editor. You can use any command line text editor of your choice.

$ vim hello_world.sh

add below content to file and save it.

#!/bin/bash

echo "Hello World"

Execute Program

Now execute the above-created script. You can execute the script in various ways as follows. Read below methods that are correct and which is wrong.

$ sh hello_world.sh        ## Wrong method
$ ./hello_world.sh         ## Correct method
$ bash hello_world.sh      ## Correct method
  • The first way is wrong because you are asking shell to use the sh interpreter (not bash).
  • The second is right because executing the script without proving any shell information. Now the script will use shell defined with Shebang (#!/bin/bash) interpreter.
  • The third will also use bash as an interpreter, so that is fine for our script.
  • Let’s execute the script without any shell. Simple typescript names as follows.

    $ ./hello_world.sh
    
    -bash: ./hello_world.sh: Permission denied
    

    Oops, what happened now? Not to worry, this error occurred due to permission. Your script doesn’t have to execute permission. So use the below commands to set execute permission on the script and then run.

    $ chmod +x ./hello_world.sh
    $ ./hello_world.sh
    
    Hello World