techboard.io

Discover Bash Programming !!!

2 years ago, Written by , Posted in Unix
bash

If you have never programmed in Bash before , Time has come to start …

Bash : Bourne Again SHell

Bash is not only an excellent command line shell, but a scripting language in itself. Shell scripting allows you to fully use the shell’s abilities and to automate a lots of tasks that would otherwise require many commands and time to perform. A lot of programs lying around your Linux box are shell scripts. If you are interested in learning how they work, or in modifying them, it is essential that you understand the bash syntax and semantics. In addition, by understanding this language, you will be able to write your own programs to do things exactly the way you want them done (isn’t it crazy 🙂 ).

Your first Bash program

Our first program will be the classical “Hello World” program. Yes, if you have programmed before, you must be sick of this by now. However, this is traditional, and who am I to change traditions? The “Hello World” program simply prints the two words “Hello World” on the screen.

So fire up your linux terminal , proceed step by step as follow :

 1: Type vi my_first_script.sh      (I will explain what Vi  is in another post … )

Insert the following inside it:

#!/bin/bash
echo “Hello World”

The first line tells Linux to use the bash interpreter to run this script. In this case, bash is in the /bin directory. If bash is in a different directory on your system, make the appropriate changes to the line. Explicitly specifying the interpreter is very important, so be sure you do it as it tells Linux which interpreter to use to run the instructions in the script.

 With that done, you need to make the script executable

2:$ chmod +x ./hello.sh  ( explanation  in another post I Promise)

3:Launch the script …

$ ./hello.sh
Hello World 

 

Note that you have this “./”  before hello.sh  ———-> so it will be executed using the command interpreter specified in the shebang  #!/bin/bash

 

There it is! Your first program!

Boring and useless as it is, this is how everyone starts out. Just remember the process here. Write the code, save the file, and make it executable with chmod.

 

Latest posts by Abdelghani EL FETOUHY (see all)

Leave a Reply

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