Hello World, Bash!

LinuxLinuxBeginner
Practice Now

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

Introduction

This lab will guide you through creating a simple shell script that prints the classic "Hello, World!" message. You will learn the fundamentals of shell programming using the Bash shell, which is widely used in Unix-like operating systems such as Linux. This lab is designed for beginners, so don't worry if you're new to programming or using the command line.


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL linux(("`Linux`")) -.-> linux/BasicFileOperationsGroup(["`Basic File Operations`"]) linux(("`Linux`")) -.-> linux/BasicSystemCommandsGroup(["`Basic System Commands`"]) linux(("`Linux`")) -.-> linux/FileandDirectoryManagementGroup(["`File and Directory Management`"]) shell(("`Shell`")) -.-> shell/BasicSyntaxandStructureGroup(["`Basic Syntax and Structure`"]) linux/BasicFileOperationsGroup -.-> linux/cat("`File Concatenating`") linux/BasicSystemCommandsGroup -.-> linux/echo("`Text Display`") linux/FileandDirectoryManagementGroup -.-> linux/pwd("`Directory Displaying`") linux/BasicFileOperationsGroup -.-> linux/ls("`Content Listing`") 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/cat -.-> lab-385381{{"`Hello World, Bash!`"}} linux/echo -.-> lab-385381{{"`Hello World, Bash!`"}} linux/pwd -.-> lab-385381{{"`Hello World, Bash!`"}} linux/ls -.-> lab-385381{{"`Hello World, Bash!`"}} linux/touch -.-> lab-385381{{"`Hello World, Bash!`"}} linux/chmod -.-> lab-385381{{"`Hello World, Bash!`"}} shell/shebang -.-> lab-385381{{"`Hello World, Bash!`"}} shell/comments -.-> lab-385381{{"`Hello World, Bash!`"}} shell/quoting -.-> lab-385381{{"`Hello World, Bash!`"}} end

Familiarize Yourself with the WebIDE (VS Code)

For this Shell scripting course, we'll be using a WebIDE based on Visual Studio Code (VS Code). This integrated development environment provides a convenient way to edit scripts and access the terminal all in one place.

Accessing the WebIDE

When you start the lab, you'll see the WebIDE interface in your browser. It consists of several key parts:

  1. File Explorer (left sidebar): Shows the directory structure and files.
  2. Editor (main area): Where you'll write and edit your scripts.
  3. Terminal (bottom panel): Where you'll run commands and execute scripts.

Opening the Terminal

To open the terminal in the WebIDE:

  1. Click on "Terminal" in the top menu.
  2. Select "New Terminal" from the dropdown.

This will open a new terminal panel at the bottom of the WebIDE. You'll see a command prompt that looks similar to this:

labex@ubuntu:~/project$

This indicates that you're logged in as the user labex, on a machine named ubuntu, and your current directory is ~/project (which is shorthand for /home/labex/project).

Using the Terminal

alt text

You can use this terminal just like you would use a regular terminal on a Linux system. For example, to see the contents of your current directory, you can type:

ls

And press Enter. This will list all files and directories in your current location.

Create a Shell Script File

Now, let's create a new file for our shell script. We'll use the touch command in the terminal to do this. The touch command is used to create empty files or update the access and modification times of existing files.

Type the following command in the terminal and press Enter:

touch hello.sh

This command creates an empty file named hello.sh in your current directory. The .sh extension is commonly used for shell scripts, but it's not mandatory.

You won't see any output after running this command. In Unix-like systems, no output usually means the command was successful.

Alternatively, you can create the file using the WebIDE interface:

  1. In the File Explorer, right-click in the /home/labex/project directory.
  2. Select "New File" from the context menu.
  3. Type the filename hello.sh and press Enter.

This will create a new file named hello.sh and open it in the editor.

Edit the Shell Script

Now that we've created the file, let's add some content to it. We'll use the WebIDE's built-in editor for this task.

If the file isn't already open in the editor:

  1. In the File Explorer, double-click on hello.sh to open it.

In the editor, type the following two lines exactly as shown:

#!/bin/bash
echo 'Hello, World!'

Let's break down what these lines mean:

  1. #!/bin/bash - This is called a "shebang" line. It tells the system which interpreter should be used to run this script. In this case, we're specifying the Bash shell.
  2. echo 'Hello, World!' - This line uses the echo command to print the text "Hello, World!" to the screen.

After typing these lines, save the file by pressing Ctrl + S or by going to File > Save in the top menu.

Make the Script Executable

Before we can run our script, we need to make it executable. In Unix-like systems, files have permissions that control who can read, write, or execute them. By default, new files are not executable.

To make our script executable, we use the chmod command (which stands for "change mode"). Type the following command in the terminal and press Enter:

chmod +x hello.sh

Here's what this command does:

  • chmod is the command to change file permissions
  • +x means "add execute permission"
  • hello.sh is the name of our file

You won't see any output from this command if it's successful.

Execute the Script

Now that our script is executable, we can run it. To run a script in the current directory, we use ./ before the script name. This ./ tells the system to look for the script in the current directory.

Type the following command in the terminal and press Enter:

./hello.sh

If everything has been done correctly, you should see the output:

Hello, World!
alt text

Congratulations! You've just run your first shell script.

View the Script Contents

As a final step, let's view the contents of our script file to confirm everything is correct. We can do this using the cat command, which displays the contents of a file in the terminal.

Type the following command in the terminal and press Enter:

cat hello.sh

You should see the contents of your script displayed:

#!/bin/bash
echo 'Hello, World!'

This is a good habit to get into when working with scripts – always double-check your work!

Summary

In this lab, you have successfully created and executed a simple Bash shell script. You've learned how to:

  1. Navigate the WebIDE and use its integrated terminal
  2. Create a new script file using either the touch command or the WebIDE interface
  3. Edit a file using the WebIDE's built-in editor
  4. Understand the purpose of the shebang line in shell scripts
  5. Use the echo command to print text
  6. Make a script executable using the chmod command
  7. Run a shell script from the command line
  8. View the contents of a file using the cat command

These fundamental skills form the basis for more advanced shell scripting and automation tasks in Unix-like environments. As you continue to learn, you'll discover how powerful and flexible shell scripting can be for managing systems and automating tasks.

Remember, practice is key in programming. Try modifying the script to print different messages or create new scripts to perform other simple tasks. Don't be afraid to experiment – that's how you learn!

Other Linux Tutorials you may like