Operating Systems & Administration

[Day 5] 3. Shell Scripting or Shell Programming

D
DITISS Admin
••5 min read

Shell Scripting Overview

Shell scripting is interpreted by the shell itself and is mainly used for:

  • Automation
  • Configuration
  • Writing security patches
  • Building larger software
  • Providing development support

A Shell Script is essentially a set of commands combined with programming constructs like variables, functions, conditionals, and loops. The file extension for shell scripts is

.sh
.


1. How Shell Scripts Work

  • Like Python, shell scripts are read line by line (from top to bottom).
  • To run the script, we use
    bash
    .
  • However, the script isn’t executable by default; we need to give it execution permissions using the
    chmod
    command.
  • After adding execution permissions, we can run the script with
    ./scriptname.sh
    .

Internally, a bash program runs the script.

Note: Features may vary from shell to shell, so scripts written for one shell might not run correctly on another.


2. Shebang Line (
#!
)

  • If you are using only one shell, the Shebang Line is optional.
  • However, it’s considered good practice to include it, especially if using multiple shells.
  • The Shebang Line indicates the interpreter to be used for running the script (e.g.,
    #!/bin/bash
    ).

3. Basic Commands

echo
Command

  • By default:
    echo
    outputs a newline character at the end.
  • To avoid newlines: Use the
    -n
    option (e.g.,
    echo -n
    ).
  • Escape sequences: Don’t work by default in
    echo
    . Use the
    -e
    option to enable them.

Comments

  • In shell scripts, comments are written with
    #
    .
  • In the Shebang Line, use
    #!
    .

4. Variables in Shell Scripting

  • Shell script variables don’t have data types.

  • Declaring a variable:

    <variable_name>=<value>

    (No spaces around the equals sign)

  • Using a variable:

    $<variable_name>

  • String values: Can be written with or without single or double quotes.

In shell scripting, variables are often referred to as parameters, and parameter substitution is when the variable name is replaced with its value.


5. Environment Variables

  • Environment variables can be accessed globally, from any shell.
  • They store environment information and are written in uppercase (e.g.,
    PATH
    ).
  • To view environment variables: Use the
    env
    command.

Example:

  • PATH
    : Specifies directories where executable programs are located.

!3. Env.png


6.
man
Command

The

man
command shows the manual for Linux commands. Different sections include:

  • 1: Executable programs or shell commands
  • 2: System calls
  • 3: Library calls
  • 4: Special files
  • 5: File formats and conventions
  • 6: Games
  • 7: Miscellaneous
  • 8: System administration commands
  • 9: Kernel routines

To view the library call for the

printf
function:
man 3 printf


7. User Input in Shell Scripts

  • Using
    read
    :
    read <variable>
    • Allows you to take input from the user.
    • You can read multiple variables in one line.

8. Arithmetic Operations

expr
Command

  • Used for basic arithmetic (integers only, not floats).
  • Multiplication: Requires the
    *
    to be escaped (
    \*
    ), as it’s a meta-character in shell scripts.

bc
Command

  • The
    bc
    command
    (basic calculator) is used for more advanced arithmetic, including floats.
  • Redirection can be used with
    bc
    to output calculations to a file or standard output.

Example:

echo 1.5 + 1.5 | bc
Output:
3.0


9. Command Substitution

  • Command substitution replaces a command with its output.

    Two ways to use it:

    1. $(command)
    2. `command`

10. Conditional Statements in Shell Scripting

if
Statements:

if [ condition ] then ... elif [ condition ] then ... else ... fi

  • Note: Indentation is optional, but recommended for readability.

case
Statements:

case $ in 1|4) ... ;; 2) ... ;; *) ... ;; esac

  • *
    : Acts as the default case.
  • Cases can include integers, floats, or strings (e.g., "one", "two").

Read Next

8/26/2026

[Day 6] 1. Spacing in Shell Scripting

#### **Assignment Operator** - When writing a variable assignment, there should **not** be any spaces between the variable name, assignment operato...

8/26/2026

[Day 6] 2. Types of Kernel

### **1. Monolithic Kernel** A **monolithic kernel** is an operating system architecture where the entire kernel operates as a single large program...