Hello World Bash Shell Script

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 on Linux and Unix-like systems.

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:
    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:
    sh hello.sh
    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:

# 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


Next create a program named hello2.sh to display current date and computer/system 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"
echo ""

Run it as follows:
chmod +x 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:

# 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 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


You learned how to write “Hello World” shell script and run it on Linux or Unix-like systems. For more info see Linux Shell Scripting Tutorial and bash command man page:
man bash
help read

🐧 Get the latest tutorials on Linux, Open Source & DevOps via RSS feed or Weekly email newsletter.

🐧 2 comments so far... add one

CategoryList of Unix and Linux commands
Disk space analyzersncdu pydf
File Managementcat
FirewallAlpine Awall CentOS 8 OpenSUSE RHEL 8 Ubuntu 16.04 Ubuntu 18.04 Ubuntu 20.04
Network UtilitiesNetHogs dig host ip nmap
OpenVPNCentOS 7 CentOS 8 Debian 10 Debian 8/9 Ubuntu 18.04 Ubuntu 20.04
Package Managerapk apt
Processes Managementbg chroot cron disown fg jobs killall kill pidof pstree pwdx time
Searchinggrep whereis which
User Informationgroups id lastcomm last lid/libuser-lid logname members users whoami who w
WireGuard VPNAlpine CentOS 8 Debian 10 Firewall Ubuntu 20.04
2 comments… add one
  • Jack Taloyr Jan 13, 2017 @ 10:03

    This was the best and really easy to follow tutorials. Thank you!

  • Jack Taloyr Aug 22, 2020 @ 7:25

    This was the best and really easy to follow tutorials. Thank you!

Leave a Reply

Your email address will not be published.

Use HTML <pre>...</pre> for code samples. Still have questions? Post it on our forum