Did You Know?
Donate to TecMint

LFCS - Linux Foundation Certified SysAdmin - Exam Preparation Guide

Understand Linux Shell and Basic Shell Scripting Language Tips – Part I

Download Your Free eBooks NOW - 10 Free Linux eBooks for Administrators

Picture speak more than words and the below picture says all about the working of Linux.


Understanding Linux Shell

Understanding Linux Shell

Read Also

  1. 5 Shell Scripts to Learn Shell Programming – Part II
  2. Sailing Through The World of Linux BASH Scripting – Part III

Understanding Linux Shell

  1. Shell: A Command-Line Interpretor that connects a user to Operating System and allows to execute the commands or by creating text script.
  2. Process: Any task that a user run in the system is called a process. A process is little more complex than just a task.
  3. File: It resides on hard disk (hdd) and contains data owned by a user.
  4. X-windows aka windows: A mode of Linux where screen (monitor) can be split in small “parts” called windows, that allow a user to do several things at the same time and/or switch from one task to another easily and view graphics in a nice way.
  5. Text terminal: A monitor that has only the capability of displaying text stuff, no graphics or a very basic graphics display.
  6. Session: Time between logging on and logging out of the system.

Types of Shell on a Standard Linux Distribution

Bourne shell : The Bourne shell was one of the major shells used in early versions and became a de facto standard. It was written by Stephen Bourne at Bell Labs. Every Unix-like system has at least one shell compatible with the Bourne shell. The Bourne shell program name is “sh” and it is typically located in the file system hierarchy at /bin/sh.

C shell: The C shell was developed by Bill Joy for the Berkeley Software Distribution. Its syntax is modelled after the C programming language. It is used primarily for interactive terminal use, but less frequently for scripting and operating system control. C shell has many interactive commands.

Beginning the Fun! (Linux Shell)

There exist thousands of commands for command-line user, how about remembering all of them? Hmmm! Simply you can not. The real power of computer is to ease the ease your work, you need to automate the process and hence you need scripts.

Scripts are collections of commands, stored in a file. The shell can read this file and act on the commands as if they were typed at the keyboard. The shell also provides a variety of useful programming features to make scripts truly powerful.

Basics of Shell Programming

  1. To get a Linux shell, you need to start a terminal.
  2. To see what shell you have, run: echo $SHELL.
  3. In Linux, the dollar sign ($) stands for a shell variable.
  4. The ‘echo‘ command just returns whatever you type in.
  5. The pipeline instruction (|) comes to rescue, when chaining several commands.
  6. Linux commands have their own syntax, Linux won’t forgive you whatsoever is the mistakes. If you get a command wrong, you won’t flunk or damage anything, but it won’t work.
  7. #!/bin/sh – It is called shebang. It is written at the top of a shell script and it passes the instruction to the program /bin/sh.

About shell Script

Shell script is just a simple text file with “.sh” extension, having executable permission.

Process of writing and executing a script

  1. Open terminal.
  2. Navigate to the place where you want to create script using ‘cd‘ command.
  3. Cd (enter) [This will bring the prompt at Your home Directory].
  4. touch hello.sh (Here we named the script as hello, remember the ‘.sh‘ extension is compulsory).
  5. vi hello.sh (nano hello.sh) [You can use your favourite editor, to edit the script].
  6. chmod 744 hello.sh (making the script executable).
  7. sh hello.sh or ./hello.sh (running the script)
Writing your First Script
# My first script

echo "Hello World!"

Save the above lines on a text file, make it executable and run it, as described above.

Sample Output

Hello World!

In the above code.

#!/bin/bash (is the shebang.)
# My first script (is comment, anything following '#' is a comment)
echo “Hello World!” (is the main part of this script)
Writing your Second Script

OK time to move to the next script. This script will tell you, your’s “username” and list the running processes.

#! /bin/bash
echo "Hello $USER"
echo "Hey i am" $USER "and will be telling you about the current processes"
echo "Running processes List"

Create a file with above codes, save it to anything you want, but with extension “.sh“, make it executable and run it, from you terminal.

Sample Output

Hello tecmint
Hey i am tecmint and will be telling you about the current processes
Running processes List
  PID TTY          TIME CMD
 1111 pts/0    00:00:00 bash
 1287 pts/0    00:00:00 sh
 1288 pts/0    00:00:00 ps

Was this cool? Writing script is as simple as getting an idea and writing pipelined commands. There are some restrictions, too. Shell scripts are excellent for concise filesystem operations and scripting the combination of existing functionality in filters and command line tools via pipes.

When your needs are greater – whether in functionality, robustness, performance, efficiency etc – then you can move to a more full-featured language.

If you already know C/Perl/Python programming language or any other programming language, learning the scripting language won’t be much difficult.

Writing your Third Script

Moving to, write our third and last script for this article. This script acts as an interactive script. Why don’t you, yourself execute this simple yet interactive script and tell us how you felt.

#! /bin/bash
echo "Hey what's Your First Name?";
read a;
echo "welcome Mr./Mrs. $a, would you like to tell us, Your Last Name";
read b;
echo "Thanks Mr./Mrs. $a $b for telling us your name";
echo "*******************"
echo "Mr./Mrs. $b, it's time to say you good bye"

Sample Output

Hey what's Your First Name?
welcome Mr./Mrs. Avishek, would you like to tell us, Your Last Name
Thanks Mr./Mrs. Avishek Kumar for telling us your name
Mr./Mrs. Kumar, it's time to say you good bye

Well this is not an end. We tried to bring a taste of scripting to you. In our future article we will elaborate this scripting language topic, rather a never ending scripting language topic, to be more perfect. Your valuable thoughts in comments is highly appreciated, Like and share us and help us to spread. Till then just chill, keep connected, stay tuned.

Read Also : 5 Shell Scripts to Learn Shell Programming – Part II

I am a major in computer science, love to research nix. I love to write codes and scripts, review distros, experiment Foss Technologies, write technical articles, Hack, of course Ethically. I am working as System Administrator (nix) for a NGO.

Your name can also be listed here. Work as a Paid freelancer/writer at TecMint.
Download Free eBooks
Advanced Bash-Scripting Guide
Linux Bible
A Newbie's Getting Started Guide to Linux
Ubuntu Linux Toolbox: 1000+ Commands

52 Responses

  1. satyamdhaker says:

    nice article…keep writing for us..thanks

  2. Nice, simple graphic to go with the article. Thanks.

  3. Michael Ajitsingh says:

    Great Article, Thank You :)

  4. daniel says:

    Hey there,

    great guide.

    Is it possible to write some posts for your site?

    Would be great.

    best regards,

  5. Peter says:

    Hi, keep them articles coming!
    Question: is the semicolon optional at the end of each line? Is there a rule when you have to put a ; or when you can omit it?

    • Avishek says:

      ; (semicolon) after a command forces the next command to wait till the previous command has finished.

      The best to learn is to experiment. Why don’t u remove the semicolon and run the script, this way u’ll learn something you will really proud of

  6. Mandar says:

    Yet another very nice and informative article published on this website..
    A big THANKS from my side..

  7. Lalit says:

    Hi ,

    Nice article ,But is this limit to be a perfect script writer …..Or more please write

    • Avishek Kumar says:

      Sure @ Lalit, we have already published a series of articles and will continue to produce such high quality articles, in future.

  8. kisszso says:

    Hi Avishek,

    I was going good with the first 2 script but the 3rd gives me headache.
    When I am executing the script, it gives me eror message:

    ./name.sh: line 8: s: command not found
    Hey whats time to say goodbey

    Do you know what causes the fault?

    “I have a MacbookPro”

    • Avishek says:

      @ kisszso, the script is perfectly ok. You need to copy and paste the above code, and nothing else, save it as anything.sh and run it as ./script_name.sh

      well “time to say goodbey” is a echo statement and will be printed on standard output as it is

  9. Srinivas says:

    Thank You Avishek Kumar

  10. Nice post. I am new-bee in linux and found of shell script. Your blog empowering my learning. Thanks for sharing your knowledge. Looking forward more quality post….

  11. javier says:

    Thanks. It’s very clear.

  12. chaitanya says:

    hi,im fresher.about to learn LINUX system administration.could you please provide books for me

  13. aaron says:

    Thanks man, this is really informative. Keep up the good work.

  14. Naveen says:

    its very interesting………. it is not as difficult as i was thinking……..please carry on writing…………


  15. vle says:

    Found your short lesson very helpful. I started thinking right after successfully running the script, and began to wonder if I could create two variables for Mrs and Mr. so that user can specify gender and use that input before name.

    • Avishek Kumar says:

      Pleased to know it @ vle, Thanks for your feedback. And you can create any number of variable in a script or program.

  16. vle says:

    Avishek , Thanks once again. Would I need to create variables at the beginning of script. to have user indicate gender vs. and the echo Mr./Mrs. ?

    • Avishek Kumar says:

      You can create variable at the beginning, as it is an interactive script, if you finds it difficult, you can leave a comment here.

  17. krish says:

    i’ve 2 questions.please try to answer:—–

    1. what r enviroment variables and shell variables ?
    2. what is name of log file for filesystem check and errors?

  18. senister says:

    Thank you very much for this,
    i am using ubuntu 13.10 and it took me a while to find how to get started eventually i found vim editor and after that everything was smooth and quite enjoyable
    Thank you again

  19. Koushik says:

    thanks for this nice article….. keep blogging….

  20. bluedrean says:

    nice article, Thanks~~

  21. Yuvaraj says:

    Awesome basic tutorial for beginners

  22. jshnrz says:

    nice article man. thank you for this. helped me more on developing my interest in scripting using linux.

  23. Raviteja Pinnaka says:

    Thank you AvishekKumar ur explanation is very easy and understandable

  24. vijay says:

    in third script ,

    line 3 and 5, i.e: read a; read b;
    how script know that a is avishek and b is kumar. ?

  25. jaideep says:

    great work,

  26. Nitesh Chandra says:

    great work .. nice introductory tutorial for beginners like me. :)

  27. Girish says:

    Good work, it helps a lot to the beginners. Thanks a Ton for your efforts. Hope you tecmint will bring more articles of this kind.

Leave a Reply

This work is licensed under a (cc) BY-NC | TecMint uses cookies. By using our services, you comply to use of our cookies. More info: Privacy Policy.
© 2012-2014 All Rights Reserved.