Shell Script : Intro

Are you a Linux user? If yes, you must be familiar with a black-board look-a-like thing called command-line.

Being a Linux user means you must play around with the command-line. Like it or not, there are just some things that are done much more easily via this interface than by pointing and clicking. The more you use and learn the command-line, the more you see its potential.

Now, some of you who used Windows before using Linux may remember batch files. These were little text files that you could fill with commands to execute and Windows would run them in turn.

What is Shell?

A Shell provides you with an interface to the Unix system. It gathers input from you and executes programs based on that input. When a program finishes executing, it displays that program's output.

Shell is an environment in which we can run our commands, programs, and also the shell scripts. There are different flavors of a shell, just as there are different flavors of operating systems. Each flavor of shell has its own set of recognized commands and functions.

What is Shell Script?

Basically script is a list of commands, which are listed in the order of execution. They also allow for far more useful functions, such as command substitution. You can invoke a command, like date, and use it’s output as part of a file-naming scheme.

You can automate backups and each copied file can have the current date appended to the end of its name. Scripts aren’t just invocations of commands, either. They’re programs in their own right. Scripting allows you to use programming functions – such as ‘for’ loops, if/then/else statements, and so forth – directly within your operating system’s interface.

My First Shell Script

At first create a new file with .sh extension, like hello_world.sh Scripts are executed like programs, and in order for this to happen they need to have the proper permissions.

Now to make our shell script executable just use chmod + command,

$ chmod +x hello_world.sh

Now we are ready to write our first shell script. Before that, there are a few guidelines you need to know.

  1. Every script should being with “#!/bin/bash”
  2. Every new line is a new command
  3. Comment lines start with a #
  4. Commands are surrounded by ()

Now we will write a simple script, that will read one input (:name) from keyboard and say hello to that person.

echo "Input your name?"
echo "Hello, $PERSON"

Lets execute our shell script like this,

$ ./hello_world.sh

Is it working? I bet yes. In our second script odd_even.sh we will a for loop and if..else statement to printout Odd and Even numbers in a range.

for n in {1..6}
   out=$(( $n % 2 ))
   if [ $out -eq 0 ]
      echo "$n is EVEN"
      echo "$n is ODD"


1 is ODD
2 is EVEN
3 is ODD
4 is EVEN
5 is ODD
6 is EVEN

Great! Now we know how to use loop, decision making and simple arithmetic in a shell script. Want to see something in shell. Run this command,

$ telnet towel.blinkenlights.nl


https://www.tutorialspoint.com/unix/index.htm https://www.howtogeek.com/67469/the-beginners-guide-to-shell-scripting-the-basics/ https://www.javatpoint.com/shell-scripting-tutorial

All Rights Reserved

Let's register a Viblo Account to get more interesting posts.