BCA / B.Tech 11 min read

Subprograms (Functions) in Linux Shell Scripting

Subprograms in Linux:


A subprogram, often known in programming as a function or modular programming, is an independent block of code used within a larger program. This code block, which can be called repeatedly, is designed to perform a specific task. The main purpose of a subprogram is to divide code into smaller modules to avoid repetition and improve the program's structure.

In Linux shell scripting, subprograms are called functions. A function is a block of code in a shell script that can be called at any time, allowing for code reuse and better management.

Structure of a Function: A function in a shell script has a simple structure. It is a block of code given a name, which can then be called by that name. Functions can have input parameters (arguments) and can also return an output. The syntax is: `function_name() { # code block }`.

Arguments in Functions: Arguments can be passed to a function as input. They are accessed via `$1`, `$2`, `$3`, etc., representing the inputs given at the time of the function call.

Returning Values from Functions: In shell scripting, `return` can be used to return a value from a function, but it can only return values from 0 to 255. If we need to return larger values or strings, `echo` is used. The output of the `echo` can then be captured in a variable.

Control Flow in Functions: Control flow, such as `if-else`, `for-loop`, and `while-loop`, can also be used within functions to implement complex decisions and loops.

Uses of Functions: Functions are used to make code simple and reusable, organize the code by breaking down large programs into smaller tasks, and reduce the complexity of scripts.

Advantages and Disadvantages of Functions:
Advantages: Reusability, better organization, easier to understand, and easier debugging.
Disadvantages: Can use more resources, and overuse can sometimes make the system more complex.

In this Chapter

Subprograms (Functions) in Linux Shell Scripting
Distributed System in Operating System
Real-Time System in Operating System
System Calls in Operating System
System Programs in Operating System
Structure of an Operating System
Layered Design of an Operating System Structure
UNIX in Operating System
Virtual Machine in Operating System
Kernel-Based Operating System
Process Concept in Operating System
Interacting Processes in Operating System
Threads in Operating System
Fundamentals of Scheduling in Operating System
Scheduling Criteria in Operating System
Long, Medium, and Short-Term Scheduling
Scheduling Algorithms in Operating System
Structure of a Concurrent System
Critical Region in Operating System
Critical Section in Operating System
Inter-process Communication (IPC) in Operating System
Monitors in Operating System
Semaphores in Operating System
Semaphore Implementation & Uses in Operating System
Logical and Physical Address in Operating System
Swapping in Operating System
Contiguous Allocation in Operating System
Segmentation in Operating System
Paging in Operating System
Virtual Memory using Segmentation in Operating System
Interprocess Communication Protocol in Operating System
Network Operating System
Design Issues in Distributed File System
Network Structure in Operating System
Structure of a Distributed System
File System and Coordination in Operating System
History of Linux in Operating System
Linux Commands
Programmer Interface & File Manipulation in Linux
Process Control in Linux
Kernel & Signals in Linux
File System in Linux
Blocks and Inodes in Linux
System Editors in Linux
Character Transliteration in Linux
The `ed` Line Editor in Linux
The `vi` Editor and Its Commands
Shell Scripting in Linux
Looping & Decision Making in Linux Shell Scripting
Variables & File Name Expansion in Linux
Arrays in Linux Shell Scripting
C Interface with Linux
Simple Shell Programs in Linux
BCA Part-1 | Operating Systems | Semester-I | MDSU Exam Paper 2023 (Held in 2024)
What is an Operating System (OS)
Operating System (OS) All Important Questions and Answers in English (MDSU)
BCA || Operating System 2025 Paper || MDSU Exam Paper
BCA | OS(Operating System) 2023 Paper | MDSU Exam Paper
Types of Operating Systems
Goals of an Operating System
Operations of an Operating System
Resource Allocation & Functions in an Operating System
Classes of Operating System
Batch Processing in Operating System
Multiprocessing in Operating System
Time-Sharing in Operating System