Hello World in Bash
Bash (Bourne Again Shell) is a command language interpreter for the written Unix-like systems written by Brian Fox for the GNU Project. It is a free software application that replaces the Bourne Shell (sh) interpreter.
Before starting with your first script, you must know about Shebang’s character.
What is Shebang (#!)
A Shebang is declared at the very first line of any shell script starting with the “#!” symbols and followed by the shell interpreter. This tells the system which interpreter is required to run this script only if not defined at the command line during runtime.
The above method is correct and works properly. Here is another way to define shebang characters with the environment. This is helpful for creating cross-environment scripts.
Here the script will automatically find the bash location instead of defining a fixed location in the first example.
To read more about shebang visit here.
Write Hello World Program
Now create a new file and edit it in a text editor. You can use any command line text editor of your choice.
add the below content to the file and save it.
Execute Program
Now execute the above-created script. You can execute the script in various ways as follows. Read below the methods that are correct and which are wrong.
- The first way is wrong because you are asking shell to use the sh interpreter (not bash).
- The second is right because executing the script without proving any shell information. Now the script will use shell defined with Shebang (#!/bin/bash) interpreter.
- The third will also use bash as an interpreter, so that is fine for our script.
Let’s execute the script without any shell. Simple typescript names are as follows.
Oops, what happened now? Not to worry, this error occurred due to permission. Your script doesn’t have to execute permission. So use the below commands to set execute permission on the script and then run.
0 Comments
CAN FEEDBACK
Emoji