Hello World, Bash!

LinuxLinuxBeginner
Practice Now

This tutorial is from open-source community. Access the source code

Introduction

This lab will guide you through the process of creating a simple shell script that prints the famous "Hello, World!" message. You will learn the basics of shell programming using the Bash shell.


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL linux(("`Linux`")) -.-> linux/BasicSystemCommandsGroup(["`Basic System Commands`"]) linux(("`Linux`")) -.-> linux/BasicFileOperationsGroup(["`Basic File Operations`"]) shell(("`Shell`")) -.-> shell/BasicSyntaxandStructureGroup(["`Basic Syntax and Structure`"]) linux/BasicSystemCommandsGroup -.-> linux/echo("`Text Display`") linux/BasicFileOperationsGroup -.-> linux/touch("`File Creating/Updating`") linux/BasicFileOperationsGroup -.-> linux/chmod("`Permission Modifying`") shell/BasicSyntaxandStructureGroup -.-> shell/shebang("`Shebang`") shell/BasicSyntaxandStructureGroup -.-> shell/comments("`Comments`") shell/BasicSyntaxandStructureGroup -.-> shell/quoting("`Quoting Mechanisms`") subgraph Lab Skills linux/echo -.-> lab-153893{{"`Hello World, Bash!`"}} linux/touch -.-> lab-153893{{"`Hello World, Bash!`"}} linux/chmod -.-> lab-153893{{"`Hello World, Bash!`"}} shell/shebang -.-> lab-153893{{"`Hello World, Bash!`"}} shell/comments -.-> lab-153893{{"`Hello World, Bash!`"}} shell/quoting -.-> lab-153893{{"`Hello World, Bash!`"}} end

Create a Shell Script File

Open a text editor and create a new file named ~/project/hello.sh. This file will contain the shell script.

touch ~/project/hello.sh
alt text

Add the Shebang and Comment

The first line of the shell script file is called the "shebang".

It specifies the shell interpreter to be used for executing the script. In this case, we will use Bash.

Add the following line at the beginning of the hello.sh file:

#!/bin/bash

This line tells the operating system that the script should be executed using the Bash shell.

Add the Echo Command

The echo command is used to print messages to the screen. We will use it to print the "Hello, World!" message.

Add the following line below the shebang line:

echo 'Hello, World!'

This line will print the message "Hello, World!" to the screen.

The single quotes are used to enclose the message. You can also use double quotes, but single quotes are preferred in this case because the message does not contain any variables or special characters.

Save the hello.sh file and close the text editor. We will run the script in the next step to see the output.

Make the Script Executable

In order to execute the script, we need to give it execute permissions. Open a terminal and navigate to the directory where the hello.sh file is located.

Run the following command to make the script executable:

chmod +x hello.sh
alt text

Execute the Script

Now, we can execute the shell script by running the following command:

./hello.sh

The script will be executed and the "Hello, World!" message will be printed to the screen.

Summary

Congratulations! You have successfully created a simple shell script that prints the "Hello, World!" message. You have learned the basics of shell programming using the Bash shell and executed your first script. Shell scripting is a powerful tool for automating tasks and managing systems.

Other Linux Tutorials you may like