Best Shell Scripting Tutorial | Quickstart [ 2020 ] - MUST-READ
Shell Scripting Tutorial

Best Shell Scripting Tutorial | Quickstart – MUST-READ

Last updated on 07th Jul 2020, Blog, Tutorials

About author

Kowsalya Sekar (Mean Stack Developer )

Kowsalya Sekar has over four years of experience as a project estimator. She provides extensive expertise in HTTP/2, JavaScript, ES6, Micro Services, Performance Engineering, Docker, Kubernetes, AZURE, CI/CD pipeline, and MySQL Database design.

(5.0) | 19620 Ratings 1335

Shell Scripting tutorial provides basic and advanced concepts of Shell Scripting. Our Shell Scripting tutorial is designed for beginners and professionals.

Shell Scripting is an open-source operating system.

Our Shell Scripting tutorial includes all topics of Scripting executing scripting, loops, scripting parameters, shift through parameters, sourcing, getopts, case, eval, let etc. There is also given Shell Scripting interview questions to help you better understand the Shell Scripting operating system.

  • A shell is special user program which provide an interface to user to use operating system services. Shell accept human readable commands from user and convert them into something which kernel can understand. It is a command language interpreter that execute commands read from input devices such as keyboards or from files. The shell gets started when the user logs in or start the terminal.
    linux shell

    Shell is broadly classified into two categories –
    • Command Line Shell
    • Graphical shell
Shell-Scripting

    Subscribe For Free Demo

    [custom_views_post_title]

    Command Line Shell
    Shell can be accessed by user using a command line interface. A special program called Terminal in linux/macOS or Command Prompt in Windows OS is provided to type in the human readable commands such as “cat”, “ls” etc. and then it is being execute. The result is then displayed on the terminal to the user. A terminal in Ubuntu 16.4 system looks like this –
    linux command line
    In above screenshot “ls” command with “-l” option is executed.
    It will list all the files in current working directory in long listing format.
    Working with command line shell is bit difficult for the beginners because it’s hard to memorize so many commands. It is very powerful, it allows user to store commands in a file and execute them together. This way any repetitive task can be easily automated. These files are usually called batch files in Windows and Shell Scripts in Linux/macOS systems.
    Graphical Shells
    Graphical shells provide means for manipulating programs based on graphical user interface (GUI), by allowing for operations such as opening, closing, moving and resizing windows, as well as switching focus between windows. Window OS or Ubuntu OS can be considered as good example which provide GUI to user for interacting with program. User do not need to type in command for every actions.A typical GUI in Ubuntu system –

    • There are several shells are available for Linux systems like –
      • BASH (Bourne Again SHell) – It is most widely used shell in Linux systems. It is used as default login shell in Linux systems and in macOS. It can also be installed on Windows OS.
      • CSH (C SHell) – The C shell’s syntax and usage are very similar to the C programming language.
      • KSH (Korn SHell) – The Korn Shell also was the base for the POSIX Shell standard specifications etc.

    Each shell does the same job but understand different commands and provide different built in functions.

    • Shell Scripting
      Usually shells are interactive that mean, they accept command as input from users and execute them. However some time we want to execute a bunch of commands routinely, so we have type in all commands each time in terminal.
      As shell can also take commands as input from file we can write these commands in a file and can execute them in shell to avoid this repetitive work. These files are called Shell Scripts or Shell Programs. Shell scripts are similar to the batch file in MS-DOS. Each shell script is saved with .sh file extension eg. myscript.sh
      A shell script have syntax just like any other programming language. If you have any prior experience with any programming language like Python, C/C++ etc. it would be very easy to get started with it.
      A shell script comprises following elements –
    • Shell Keywords – if, else, break etc.
    • Shell commands – cd, ls, echo, pwd, touch etc.
    • Functions
    • Control flow – if..then..else, case and shell loops etc.
    • Why do we need shell scripts
      There are many reasons to write shell scripts –
      • To avoid repetitive work and automation
      • System admins use shell scripting for routine backups
      • System monitoring
      • Adding new functionality to the shell etc.
      • Advantages of shell scripts
        • The command and syntax are exactly the same as those directly entered in command line, so programmer do not need to switch to entirely different syntax
        • Writing shell scripts are much quicker
        • Quick start
        • Interactive debugging etc.
        • Disadvantages of shell scripts
            • Prone to costly errors, a single mistake can change the command which might be harmful
            • Slow execution speed
            • Design flaws within the language syntax or implementation
            • Not well suited for large and complex task
            • Provide minimal data structure unlike other scripting languages. etc

          Scripting Guidelines

          Scripting-Guidelines

          As I mentioned before, every script file is essentially plain text. That doesn’t mean you can write what you want all willy-nilly, though. When a text file is attempted to be executed, shells will parse through them for clues as to whether they’re scripts or not, and how to handle everything properly. Because of this, 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 ()

          The Hash-Bang Hack

          When a shell parses through a text file, the most direct way to identify the file as a script is by making your first line:

          #!/bin/bash

          If you use another shell, substitute its path here. Comment lines start with hashes (#), but adding the bang (!) and the shell path after it is a sort of hack that will bypass this comment rule and will force the script to execute with the shell that this line points to.

          • New Line = New Command

          Every new line should be considered a new command, or a component of a larger system. If/then/else statements, for example, will take over multiple lines, but each component of that system is in a new line. Don’t let a command bleed over into the next line, as this can truncate the previous command and give you an error on the next line. If your text editor is doing that, you should turn off text-wrapping to be on the safe side. You can turn off text wrapping in nano bit hitting ALT+L.

          Comment Often with #s

          If you start a line with a #, the line is ignored. This turns it into a comment line, where you can remind yourself of what the output of the previous command was, or what the next command will do. Again, turn off text wrapping, or break you comment into multiple lines that all begin with a hash. Using lots of comments is a good practice to keep, as it lets you and other people tweak your scripts more easily. The only exception is the aforementioned Hash-Bang hack, so don’t follow #s with !s. 😉

          Commands Are Surrounded By Parentheses

          In older days, command substitutions were done with single tick marks (`, shares the ~ key). We’re not going to be touching on this yet, but as most people go off and explore after learning the basics, it’s probably a good idea to mention that you should use parentheses instead. This is mainly because when you nest – put commands inside other commands – parentheses work better.

          Your First Script

          Let’s start with a simple script that allows you to copy files and append dates to the end of the filename. Let’s call it “datecp”. First, let’s check to see if that name conflicts with something:

          simple-script

          You can see that there’s no output of the which command, so we’re all set to use this name.

          Let’s create a blank file in the ~/bin folder:

          touch ~/bin/datecp

          hashbang-hack

          And, let’s change the permission now, before we forget:

          change-permission

          Let’s start building our script then. Open up that file in your text editor of choice. Like I said, I like the simplicity of nano.

          Course Curriculum

          Enroll in Unix Shell Scripting Certification Training Course & Get Noticed By Top Hiring Companies

          Weekday / Weekend BatchesSee Batch Details

          nano ~/bin/datecp

          And, let’s go ahead and put in the prerequisite first line, and a comment about what this script does.

          hashbang-hack

          Next, let’s declare a variable. If you’ve ever taken algebra, you probably know what a that is. A variable allows us to store information and do things with it. Variables can “expand” when referenced elsewhere. That is, instead of displaying their name, they will display their stored contents. You can later tell that same variable to store different information, and any instruction that occurs after that will use the new information. It’s a really fancy placeholder.

          What will we put in out variable? Well, let’s store the date and time! To do this, we’ll call upon the date command.

          Take a look at the screenshot below for how to build the output of the date command:

          output-date-command

          You can see that by adding different variables that start with %, you can change the output of the command to what you want. For more information, you can look at the manual page for the date command.

          Let’s use that last iteration of the date command, “date +%m_%d_%y-%H.%M.%S”, and use that in our script.

          script

          If we were to save this script right now, we could run it and it would give us the output of the date command like we’d expect:

          command

          But, let’s do something different. Let’s give a variable name, like date_formatted to this command. The proper syntax for this is as follows:

          variable=$(command –options arguments)

          And for us, we’d build it like this:

          date_formatted=$(date +%m_%d_%y-%H.%M.%S)

          command-substitution

          This is what we call command substitution. We’re essentially telling bash that whenever the variable “date_formatted” shows up, to run the command inside the parentheses. Then, whatever output the commands gives should be displayed instead of the name of the variable, “date_formatted”.

          Here’s an example script and its output:

          echo-date-script
          echo-date-output

          Note that there are two spaces in the output. The space within the quotes of the echo command and the space in front of the variable are both displayed. Don’t use spaces if you don’t want them to show up. Also note that without this added “echo” line, the script would give absolutely no output.

          Let’s get back to our script. Let’s next add in the copying part of the command.

          cp –iv $1 $2.$date_formatted

          Course Curriculum

          Enhance Your Career with Shell Scripting Training from Real Time Experts

          • Instructor-led Sessions
          • Real-life Case Studies
          • Assignments
          Explore Curriculum

          appended-filename

          This will invoke the copy command, with the –i and –v options. The former will ask you for verification before overwriting a file, and the latter will display what is being down on the command-line.

          Next, you can see I’ve added the “$1” option. When scripting, a dollar sign ($) followed by a number will denote that numbered argument of the script when it was invoked. For example, in the following command:

          cp –iv Trogdor2.mp3 ringtone.mp3

          The first argument is “Trogdor2.mp3” and the second argument is “ringtone.mp3”.

          Looking back at our script, we can see that we’re referencing two arguments:

          xappended-filename-script

          This means that when we run the script, we’ll need to provide two arguments for the script to run correctly. The first argument, $1, is the file that will be copied, and is substituted as the “cp –iv” command’s first argument.

          The second argument, $2, will act as the output file for the same command. But, you can also see that it’s different. We’ve added a period and we’ve referenced the “date_formatted” variable from above. Curious as to what this does?

          Here’s what happens when the script is run:

          Unix Shell Scripting Sample Resumes! Download & Edit, Get Noticed by Top Employers! Download
          xappended-filename-output

          You can see that the output file is listed as whatever I entered for $2, followed by a period, then the output of the date command! Makes sense, right?

          Now when I run the datecp command, it will run this script and allow me to copy any file to a new location, and automatically add the date and time to end of the filename. Useful for archiving stuff!

          CONCLUSION:

          Shell scripting is at the heart of making your OS work for you. You don’t have to learn a new programming language to make it happen, either. Try scripting with some basic commands at home and start thinking of what you can use this for.

    Are you looking training with Right Jobs?

    Contact Us
    Get Training Quote for Free