Bash Scripting Tutorial : Hello World

By | November 22, 2014

Bash ? What is Bash ? What is Bash Scripting ?

What is bash ? bash is a unix shell for GNU Operating System written by Brian Fox released in 1989 as a replacement for Bourne Shell. The name is an acronym, a pun, and a description and it stands for Bourne-Again SHell. bash is a command processor that runs in a text window, where user types command that cause actions. bash also can read command from a file, this file is called a script. If you write your bash script on a file, this progress called bash scripting.

bash has been distributed widely as the default shell for the GNU Operating System Linux and Mac OS X ,f or Microsoft Windows it has been ported with Cygwin and MinGW and for Android you can try via various terminal emulation applications on the market.

What you can do with bash ?

bash is not only an excellent command shell, but a scripting language in itself, with shell scripting, you can fully utilize the shell’s abilities to automate and run a lot of tasks that require a lot of commands to perform. Every programs that lying around on your Linux are shell scripts, by understanding the bash language, you will be able to write and create your own programs to do things the way you want, for example if you want to shutdown your computer automatically after your download is complete, you want to create some special pattern like the star formation, or you want to create a program that checking your server utilization.

What do you need to know before learning bash script ?

Before writing your own shell scripts, you need to know the very basic Linux command. For example how to copy and move a file with cp and mv, create new files, and change user permission with chmod, The thing you must to know is how use a major Text Editor (vim, pico or nano) or if you not familiar with those text editor you can use your own text editor like Atom Text Editor or NotepadQQ.

Let’s do Hello World !

Before you start, please don’t practice scripting as the root user !, I will not be held reponsible if you accidentally make a mistake in the coding and screw up your system. Use a normal user account or you can create a new user just for practice scripting.

So after prepare anything you need, let’s we do some basic bash scripting. Like the others programming and scripting language, our first program will be the classical “Hello World”. So open up your own favorite text editor, in this case I will you use vim as my text editor (my favorite :D)  and let’s we start.

Step 1

First you should find where is your bash interpreter located. Open your terminal and enter this following command into your terminal :

$ which bash
Bash Scripting Tutorial : Hello World

Bash Scripting Tutorial : Hello World

Now we found where is bash interpreter is located, it’s located in /bin/bash.

Step 2

Switch to your text editor, now here is our first bash shell script and type those script :

#Interpreter location
#!/bin/bash

#print Hello World !
echo "Hello World !"

Note : Every bash script must start with “#!”, which is not read as a comment.

Bash Scripting Tutorial : Hello World 2

Bash Scripting Tutorial : Hello World 2

Now save your file with file name : whatever.sh. File extension for bash script is .sh.

Step 3

Navigate to a directory where you save whatever.sh and change it’s permissions for make the file executeable with chmod.

$ chmod +x whatever.sh

Bash Scripting Tutorial : Hello World 3

Bash Scripting Tutorial : Hello World 3

Step 4

Now we are ready to execute your first bash script, to run it just type :

$ ./whatever.sh

Bash Scripting Tutorial : Hello World 4

Bash Scripting Tutorial : Hello World 4

If you see output “Hello World !” then you are successful write our own bash script ! Congratulations !, now for the fun part I have a challenge for you. It’s just a simple challenge using echo like what we learn before.

The Challenge !

So this is the challenge part, in this challenge I want you to create a bash script that contains an image on it, not a real image though. It’s just an image from a bunch of ascii characters. So this is mine :D.

Bash Scripting Tutorial : Hello World 5

Bash Scripting Tutorial : Hello World 5

Can you do it too ?. I got my image from here, you can find any ascii art on search engine then try to insert them your bash script with echo command and show me yours on the comment box.

So that’s all for this post. If you have some suggestion or something to say about this post, or if I have some mistake on this post, you can leave a comment or contact me. Thank You ! 🙂

Leave a Reply

Your email address will not be published. Required fields are marked *