Operating Systems & Administration

[Day 7] 3. Back to Shell Scripting

D
DITISS Admin
••4 min read

Positional Parameters

Positional parameters are arguments passed to a script from the command line when you execute it. They allow you to pass data directly to the script.

Example Command:

./script27.sh 10 A sunbeam 3.142

  • $1
    ,
    $2
    ,
    $3
    , etc.
    : These refer to individual positional parameters.
    • $1
      =
      10
    • $2
      =
      A
    • $3
      =
      sunbeam
    • $4
      =
      3.142
  • $0
    : This is the name of the script itself (
    /script27.sh
    ).
  • $*
    : All positional parameters as a single string.
  • $@
    : All positional parameters as individual strings.
  • $$
    : The process ID (PID) of the current shell.
  • $#
    : The number of positional parameters.

Functions:

Functions allow you to organize and reuse code blocks inside your script. You can define a function using the

function
keyword (optional) or just parentheses
()
.

Example Syntax:

function my_function() {     echo "This is a function." }

  • Calling the function: You simply call it by name.
  • Arguments: Inside functions, arguments are accessed just like positional parameters, using
    $1
    ,
    $2
    , etc.

Example:

my_function() {     echo "First argument: $1"     echo "Second argument: $2" } my_function "Hello" "World"

This would output:

First argument: Hello Second argument: World


Arrays

In shell scripting, arrays allow you to store multiple values in a single variable. Arrays are indexed starting from 0.

Declaration:

declare -a arr arr=(5 3 6 8 2 1)

  • Accessing array elements: Use

    ${arr[index]}
    .

    echo ${arr[0]}   # prints first element (5)

  • Printing all elements:

    • ${arr[*]}
      : Prints all elements as a single string.
    • ${arr[@]}
      : Prints each element individually.
  • Number of elements: Use

    ${#arr[@]}
    to get the array length.

Example:

echo "Elements from index 2 onwards: ${arr[*]:2}" # prints from the 3rd element onwards (6 8 2 1)


Strings

In shell scripting, strings are simple variables that hold text. You can compare and manipulate strings using operators.

Common Operators

  • =
    : True if both strings are equal.
  • !=
    : True if both strings are not equal.
  • -n
    : True if the string is not empty.
  • -z
    : True if the string is empty.

String Concatenation

You can concatenate two strings as follows:

str1="Ojas" str2="Dudhabaware" str3=$str1$str2 echo "Concatenated String: $str3"   # Output: OjasDudhabaware


test
Command

The

test
command is used to check various conditions, such as file types and numeric comparisons. It returns an exit status (
0
for success, anything else for failure).

Example of Checking Exit Status:

test -f myfile.txt echo $?    # Outputs 0 if the file exists, 1 otherwise

Using

test
in an
if
statement:

if test -f "myfile.txt"; then     echo "File exists" else     echo "File does not exist" fi

By using the

echo $?
, you can also check the exit status of previous commands to see if they were successful (
0
) or if they failed (non-zero).


Read Next

8/26/2026

[Day 1] 1. Operating System

!1. OS.png ### **Operating System (OS)** An Operating System is a crucial software that enables hardware to function by acting as an interface b...

8/26/2026

[Day 1] 2. Functions of Operating System

### **Functions of an Operating System (OS)** The Operating System performs several critical functions that ensure the smooth operation of a comput...