The Ultimate Guide for Beginners on Bash Scripting / Shell Scripting step by step

Table of content

  1. What is Shell ?
  2. What is Bash ?
  3. What is Shell Scripting or Bash Scripting?
  4. How to create Shell scripts and execute it ?
  5. Basic fundamentals of Shell Scripting?
  6. Run bash scripts on Visual Studio
  7. Conclusion

What is Shell ?

Shell is a command line interpreter and a programming language, basically what ever you are executing on terminal of your Linux machine is a shell command. There are thousands of commands which are already inbuilt such as cat , cd , ls , kill , history or pwd. The shell provides variables, flow control constructs, scripts, and functions. It also allows you to pipe commands, substitute command , do conditional testing , iterations etc. Whatever scripts you run , commands you execute are executed on shell or commonly known as Unix Shell.

  • There are different types of Unix shell available:
    • Bourne shell (sh) which is present in /bin/sh or /usr/bin/sh
    • Korn shell (ksh) which is present in /bin/ksh or /usr/bin/ksh
    • Bourne Again shell (bash) which is present in /bin/bash or /usr/bin/bash
    • POSIX shell (sh)
    • C shell (csh)
    • TENEX/TOPS C shell (tcsh)

  • To check on which shell you’re
 echo $SHELL

What is Bash?

Bash is a Unix shell and also a command line interpreter. It is also known as Bourne again shell . This is improved version of Bourne shell that “sh”. This is present in almost all the operating system. It is a default login shell mostly in all Linux distributions. Also it is default login shell in Apple macOS and Solaris. Bash process shell commands. In bash you write all your commands in text format and execute commands. When bash executes any commands from other files then they can called as shell scripts.

It also contains keywords , variable , functions etc. just like sh shell . It is very much similar to Bourne shell (sh) .Latest version is bash-5.1 which was released in 2020-12-07.

To check the location of bash , you can use command.

echo $BASH

What is Shell Scripting or Bash Scripting?

Shell Script is simply a text of file with various or lists of commands that are executed even on terminal or shell one by one. But in order to make thing little easier and run together as a group and in quick time we write them in single file and run it.

Main tasks which are performed by shell scripts are : file manipulation , printing text , program execution. We can include various environmental variables in script that can be used at multiple places , run programs and perform various activities are known as wrapper scripts.

A good shell script will have comments, preceded by a pound sign or hash mark, #, describing the steps. Also we can include conditions or pipe some commands to make more creative scripts.

When we execute a shell script, or function, a command interpreter goes through the ASCII text line-by-line, loop-by-loop, test-by-test, and executes each statement as each line is reached from the top to the bottom.

How to create Shell scripts and execute it ?

Now we will create a very simple script and execute it.

  • Create a directory under /opt directory
mkdir script-demo
  • Create a file myscript.sh
touch myscript.sh
  • Edit the file
vi myscript.sh
  • Paste the code as shown in code snippet
#!/bin/bash
# This is a comment 
echo Hello World, its automateinfra.com here!
echo I am using $SHELL which is a default shell. 
  • Let us go through the code
    • #! is known as Shebang which is a syntax for a bash script. You can ignore this if you run your script by adding prefix bash . For example bash myscript.sh
    • Here #!/bin/bash or #!/usr/bin/bash declares a Bourne-Again (Bash) shell
    • Similarly for Bourne shell we can use #!/bin/sh or #!/usr/bin/sh declares a Bourne shell
    • # is a comment
    • echo is a command
  • Grant the execution permissions
chmod + x myscript.sh
  • Execute the script
./myscript.sh
  • Script has been executed successfully.

Basic fundamentals of Shell Scripting

  • Shell Scripts are case sensitive
  • To execute a function
function function-name 
{
  Commands
}
  • You can run your scripts with specific shells as
    • ksh myscript.sh
    • bash myscript.sh
    • csh myscript.sh
  • If you are running a script in a particular location you should provide absolute path and if you are running in same directory then use “./”
/home/ubuntu/myscript.sh  # Complete path

./myscript                # Run in same directory
  • Use of if loops
if [condition]
then 
   command
else
   command
fi
  • Use of for loops
for condition
do
   commands
done
  • To Create a variable we use “$” symbol and this substitutes the variable to a value.
a = 5
echo $a
  • The command-line arguments $1, $2, $3,…$9 are positional parameters, with $0 pointing to the actual command, program, shell script, or function and $1, $2, $3, …$9 as the arguments to the command
  • Let us know see Special variables
$0 is the filename of the current script.
$n These variables correspond to the arguments with which a script was invoked.
$# The number of arguments supplied to a script.
$* All the arguments are double quoted. If a script receives two arguments, $* is equivalent to $1 $2.
$@ All the arguments are individually double quoted. If a script receives two arguments, $@ is equivalent to $1 $2.
$? The exit status of the last command executed.
$$ The process number / process ID of the current shell.
$! The process number of the last background command.
Table 1.1

Run bash scripts on Visual Studio

  • From the dropdown menu of terminals select default shell
  • Then you will see Git Bash and click on it.
  • Now type a command to test if bash script works
hello automateinfra.com

Conclusion

You should now have a very sound knowledge of what is shell , what is Bash shell and Shell Scripting or Bash scripting. . Also we discussed how to work create a shell script and what are basic fundamentals or points one should know to getting started on bash scripting. Finally we ran bash script in windows machine on Microsoft’s Visual Studio Code.

This tutorial consists of all the practical’s which were done on our lab server with lots of hard work and efforts.

Please share the tutorial with everyone if you like it and hoping you get benefit out of this tutorial.

Advertisement