How do I write my first bash shell script that displays message called “Hello world” on a screen on a Linux or Unix-like system?

A “Hello, World!” bash shell script is a bash program that outputs “Hello, World!” to a user. This script illustrates the basic syntax of a bash shell scripting language for a working program. It is your very first program when you are new to a bash shell scripting.

How to write hello world bash shell script

The procedure is as follows:

  1. Create a new file called using a text editor such as nano or vi:
  2. Add the following code:
    echo "Hello World"
  3. Set the script executable permission by running chmod command:
    chmod +x
  4. Run or execute the script using following syntax:

Sample session:

Improving Hello World script

Let us create a program called as follows:

# Usage: Hello World Bash Shell Script Using Variables
# Author: SXI ADMIN
# -------------------------------------------------
# Define bash shell variable called var 
# Avoid spaces around the assignment operator (=)
var="Hello World"
# print it 
echo "$var"
# Another way of printing it
printf "%sn" "$var"

Run it as follows:
chmod +x


Next create a program named to display current date and computer name as follows:

var="Hello World"
# Run date and hostname command and store output to shell variables
# print it or use the variable
# Variable names are case sensitive $now and $NOW are different names
echo "$var"
echo "Current date and time : $now"
echo "Computer name : $computer_name"

Run it as follows:
chmod +x

Reading a value from input

Our final hello world program reads input from the keyboard using the read command. Create a bash shell script named
$ nano
Append the following code:

# Clear the screen
# Read input using read command
read -p "May I know your name please? " name
echo "Hello $name, let us be friends"
echo ""

Save and close the file. Run it as follows:
$ chmod +x
$ ./

Running your shell script in “debug” mode

Pass the -x and -v option:
bash -x
bash -v
bash -x -v

For more info see Linux Shell Scripting Tutorial and bash command man page:
man bash
help read

Posted by: SXI ADMIN

The author is the creator of SXI LLC and a seasoned sysadmin, DevOps engineer, and a trainer for the Linux operating system/Unix shell scripting. Get the latest tutorials on SysAdmin, Linux/Unix and open source topics via RSS/XML feed or weekly email newsletter.