Hello World Bash Shell Script

in Categories , , last updated January 21, 2018

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 hello.sh using a text editor such as nano or vi:
    nano hello.sh
  2. Add the following code:
    #!/bin/bash
    echo "Hello World"
  3. Set the script executable permission by running chmod command:
    chmod +x hello.sh
  4. Run or execute the script using following syntax:
    ./hello.sh
    OR
    sh hello.sh
    OR
    bash hello.sh

Sample session:
Hello World Bash Shell Script Program

Improving Hello World script

Let us create a program called update-hello.sh as follows:

#!/bin/bash
# Usage: Hello World Bash Shell Script Using Variables
# Author: Vivek Gite
# -------------------------------------------------
 
# 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 "%s\n" "$var"

Run it as follows:
chmod +x update-hello.sh
./update-hello.sh

Where,

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

#!/bin/bash
var="Hello World"
 
# Run date and hostname command and store output to shell variables
now="$(date)"
computer_name="$(hostname)"
 
#
# 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 hello2.sh
./hello2.sh

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 hello-input.sh:
$ nano hello-input.sh
Append the following code:

#!/bin/bash
# Clear the screen
clear
 
# 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 hello-input.sh
$ ./hello-input.sh

Hello World with User Input using read command

Running your shell script in “debug” mode

Pass the -x and -v option:
bash -x hello-input.sh
bash -v hello-input.sh
bash -x -v hello-input.sh

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

Posted by: Vivek Gite

The author is the creator of nixCraft 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.

Share this on (or read 1 comments/add one below):

1 comment

    Have a question? Post it on our forum!