Learn Basics on Bash Scripting

0
290
Learn Basics on Bash Scripting

Bash Scripting Tutorial : 1

Today we are going to start a new series of tutorial to learn basics on bash scripting. So without further ado lets get started.

Bash is an scripting language, it’s a standard shell for linux users. Bash (Bourne Again Shell) was written by Brian Fox as a replacement for the Bourne shell which was a free version. Its totally similar to Bourne, but has been added few features such as command line editing, job control, indexed arrays of unlimited size and more.

It’s a very powerful as well as useful shell for all the linux professionals. Commands that work in sh also work in bash. Bash is a command processor that runs in terminal, where the user types commands and executes them. Bash can also read commands from a file, called a script with an extension .sh. It provides functional improvement over sh for both programming and interactive use. Most of the sh scripts can be run by bash without modification.

Here are few examples that can help you understand bash shell scripting better. So lets get into it:

echo

echo is a built-in command in bash. The syntax for echo is:

The items in square brackets are optional as long as you don’t require them. A string is any finite sequence of characters (i.e., letters, numerals, symbols and punctuation marks). When used without any options or strings, echo returns a blank line on the screen followed by the command prompt on the subsequent line. This is because pressing the ENTER key is a signal to the system to start a new line, and thus echo repeats this signal.

The output on the screen will be:

To make your script interact with user, we can use read command with a variable called ans

Save the above script with .sh extension and execute the script using ./filename.sh. Also don’t forget to provide the execution permission to the file before executing it.

Once the above script gets executed it will then ask you, what is your name ??. Now you need to type the answer and then the script will get terminated. That’s it! that’s how the bash shell executes the scripts.

Similarly, if you want to output your name into the terminal screen then we can make use of $ sign and provide the variable name i.e. ans to echo command:

The above script will give the folowing output :

If you want to make your output look more clear and clean then we can make use of a command called clear :

In the above script, the clear command will clear all the lines present above it and will print the output in a clean terminal window. The output will look like :

Now it will clear the screen and print :

As simple as that! This much should be enough for you to get started with bash scripting. More articles are no their way, just stay tuned.

That’s all in this article. Support us and make sure to visit other useful posts related to coding in coding section. Best of Luck Scripting!!

LEAVE A REPLY