What you need to know about HELLO WORLD Bash Script
Just like in other programming languages, "Hello world" is often the first line of code written by programmers when starting to learn Bash scripting.
Here you will find out:
- about "Hello world" Bash script
- how to write "Hello world" shell script
- what the structure of "Hello world" script is
- what mistakes when writing a Bash script "Hello world" are
Are you ready? Let's read!
What is a "Hello world" Bash script?
"Hello world" is the first program you will write in Bash shell scripting. This program will display the famous "Hello world!" phrase to any user that runs the Bash script. There are many different ways to write a Bash script that will display "Hello World!" to users.
Steps to writing a "Hello world" shell script
You can either use the VI or NANO text editor to write your first "Hello world" shell script in Bash. The example below shows you how to write a "hello world" script using the NANO text editor.
- Step 1: Create a new file and save it as "hello.sh"
- Step 2: Enter the following line of code
- Step 3: Run your script
There are different ways to run this script, and they are highlighted below.The simple way:
- bash hello.sh
- sh hello.sh
The advanced method:
- chmod +x hello.sh
- bash hello.sh
The structure of a "Hello world" script
This script consists of just two lines, and that’s all. However, sometimes it may have up to three lines if comments are included:
Anything was written after the hash (#) sign is treated as a comment and is ignored by the system while running the script.
So, the "Hello world" script has two lines, primarily. The first line tells the shell to execute the following command(s)/code(s) as a Bash script, while the second line is the actual command to be printed on display when a user runs the script.
Mistakes when writing a Bash script "Hello world"
Here are some common errors that may interrupt the successful output of your Bash script "Hello world":
- If you are editing the script on Windows, this will introduce some characters at the end of your script, and Bash cannot read these characters; thus, you may get the following output: command not found. However, this error can be solved using the dos2unix program; this program edits the file inline. Example:
- Some people may forget to apply to execute permissions, which may return the following output: ./hello-world.sh: Permission denied. Fix this by writing
- Bash and sh are unique shells; thus, using sh./hello-world.sh can return an error output. It is advisable to rely on the script’s shebang (#!) when writing Bash or sh. Commonly, most people use #!/usr/bin/env instead of trying to direct a path to Bash.
How to open Linux files from Windows
It is possible to access and view your Linux files while booted to Windows on a dual-boot PC or virtual machine. This is possible using DiskInternals Linux Reader, a superb freeware application that is designed to read Ext2/3/4 partitions.DiskInternals Linux Reader comes with a user-friendly interface and lets you access your Linux files easily and without any hassles. Also, DiskInternals Linux Reader is available for free for all users.