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.
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.
Open a text editor and create a new file named ~/project/hello.sh
. This file will contain the shell script.
touch ~/project/hello.sh
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.
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.
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
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.
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.