BCA / B.Tech 15 min read

Shell Script in Hindi

RK

लेखक Ratan Kumawat · संस्थापक, NotesMedia

प्रकाशित:

त्वरित सारांश और मुख्य बिंदु

  • • विषय फोकस: Operating System (OS) Notes in Hindi | ऑपरेटिंग सिस्टम नोट्स (BCA / B.Tech Syllabus)
  • • मुख्य विषय: Shell Script in Hindi - General Topics
  • • अवलोकन: Quick Answer: Shell Script एक plain text file होती है जिसमें Linux/Unix की commands एक sequence में लिखी होती हैं, जिन्हें Shell interpreter एक-एक करके...
  • • मुख्य अवधारणाएं: Shell script, Bash variables loops conditionals Hindi, Functions shell,Shell script, Bash variables loops conditionals, Functions shell
Quick Answer: Shell Script एक plain text file होती है जिसमें Linux/Unix की commands एक sequence में लिखी होती हैं, जिन्हें Shell interpreter एक-एक करके run करता है। Routine tasks (जैसे backup, system monitoring और file management) को automate करने के लिए shell scripting का इस्तेमाल होता है। Script में command-line arguments को handle करने के लिए special variables इस्तेमाल किए जाते हैं—जैसे $# (arguments की कुल संख्या), $* (सभी arguments एक single string के रूप में), और $@ (सभी arguments अलग-अलग quoted strings के रूप में)।

Shell Scripting क्या है? (What do you mean by Shell Scripting)

Linux और Unix operating system में user सीधे hardware से बात नहीं करता। User और operating system के core (kernel) के बीच एक interface काम करता है जिसे Shell कहते हैं। Shell एक command-line interpreter (CLI) है जो user से commands लेता है, उन्हें translate करता है, और kernel को execution के लिए सौंपता है। Operating system की structure detail में समझने के लिए पढ़ें: Linux.

Advertisement

जब आपको कोई एक task करना हो, तो आप terminal पर single command type करते हैं (जैसे ls, pwd, या mkdir)। लेकिन जब आपको 10 या 20 commands रोज एक ही order में run करनी हों, तो हर बार manual typing करना slow और error-prone होता है। इसी problem को solve करने के लिए Shell Script बनाई जाती है।

Shell script साधारण शब्दों में commands का एक program है। यह एक normal text file होती है (आम तौर पर .sh extension के साथ), जिसमें commands top-to-bottom order में लिखी होती हैं। जब आप script को run करते हैं, तो shell उसमें लिखी हर line को एक batch job की तरह execute करता है। इस automation process का background समझने के लिए देखें: Batch Processing.

+-------------------------------------------------------+
|                         User                          |
+-------------------------------------------------------+
                           |  (Commands / Script)
                           v
+-------------------------------------------------------+
|              Shell (bash, sh, ksh, zsh)               |  <-- Script runs here
+-------------------------------------------------------+
                           |  (System Calls)
                           v
+-------------------------------------------------------+
|                        Kernel                         |
+-------------------------------------------------------+
                           |
                           v
+-------------------------------------------------------+
|                       Hardware                        |
+-------------------------------------------------------+

Types of Shell in Unix and Linux

Unix और Linux systems में कई तरह के shells उपलब्ध हैं। हर shell का syntax, speed और feature set थोड़ा अलग होता है:

  • sh (Bourne Shell): यह Unix systems का सबसे पहला और standard shell है, जिसे Stephen Bourne ने Bell Labs में develop किया था। यह light-weight और fast है, लेकिन इसमें interactive features (जैसे command history और auto-completion) limited हैं।
  • bash (Bourne Again Shell): यह modern Linux distributions (जैसे Ubuntu, RedHat, CentOS, Kali) का default shell है। यह sh का advanced version है और इसमें command completion, command history, arrays, और powerful scripting syntax मिलता है।
  • csh (C Shell) और tcsh: इसका syntax C programming language जैसा दिखता है। इसे Bill Joy ने develop किया था। Interactive use के लिए अच्छा है, लेकिन production scripting के लिए कम recommend किया जाता है।
  • ksh (Korn Shell): इसे David Korn ने design किया था। यह Bourne shell की scripting compatibility और C shell के interactive features दोनों को मिलाता है। बड़ी enterprises के Unix servers (जैसे AIX) में इसका काफी use होता है।
  • zsh (Z Shell): यह Bash का modern और highly customizable version है। macOS ने इसे अपना default shell बनाया है। इसमें theme support और advanced tab-completion मिलता है।
Shell Name Command / Path Prompt Symbol Key Feature / Best For
Bourne Shell (sh) /bin/sh $ Small size, maximum portability across Unix systems
Bourne Again Shell (bash) /bin/bash $ (root के लिए #) Linux standard, rich features, exam programs के लिए default
Korn Shell (ksh) /bin/ksh $ Fast execution, financial and enterprise Unix servers
C Shell (csh / tcsh) /bin/csh % C programmers के लिए familiar syntax
Z Shell (zsh) /bin/zsh % Modern plugins, themes, macOS default

Shell Script कैसे बनाएं और Run करें? (Step-by-Step Guide)

Linux में एक working shell script बनाने के लिए नीचे दिए गए 5 steps follow कीजिए:

Step 1: Text Editor में File बनाएं

Terminal open कीजिए और किसी भी text editor (जैसे nano या vi) से एक file बनाइए:

nano first_script.sh

Step 2: पहली Line में Shebang (#!) लिखें

हर shell script की पहली line Shebang (#!) से शुरू होनी चाहिए। इसके बाद shell interpreter का path लिखा जाता है। यह operating system को बताता है कि file को किस program से interpret करना है:

#!/bin/bash

अगर आप shebang नहीं लिखेंगे, तो script user के current login shell में run होगी, जिससे syntax errors आ सकते हैं।

Step 3: Script का Code लिखें

Shebang के नीचे अपनी commands लिखिए, file save कीजिए और editor से बाहर आइए:

#!/bin/bash
# यह एक basic test script है
echo "Hello, Linux World!"
echo "Current Date and Time:"
date
echo "Current Working Directory:"
pwd

Step 4: Execute Permission दें (chmod +x)

Linux में default रूप से newly created files को execute permission नहीं मिलती। Script को executable बनाने के लिए chmod command चलाएं:

chmod +x first_script.sh

Linux file permissions और commands detail में सीखने के लिए देखें: Linux Commands.

Step 5: Script को Run करें

Current directory से script run करने के लिए file name के आगे ./ लगाएं:

./first_script.sh

Screen Output:

Hello, Linux World!
Current Date and Time:
Mon Nov 23 11:05:46 IST 2026
Current Working Directory:
/home/student/scripts

Special Variables: $* vs $@ vs $# in Shell Script

जब आप terminal से script call करते समय arguments pass करते हैं (जैसे ./backup.sh project1 report.pdf), तो इन्हें command-line arguments या positional parameters कहते हैं। Shell इन values को hold करने के लिए pre-defined special variables देता है:

Special Variable क्या काम करता है (Meaning) Example Value (यदि चलाया: ./test.sh apple banana cherry)
$0 चल रही script का file name ./test.sh
$1, $2, ... $9 पहला, दूसरा और 9th तक positional argument $1 = apple, $2 = banana, $3 = cherry
${10}, ${11} 9 से आगे के arguments (curly braces जरूरी हैं) 10th argument value
$# Script को pass किए गए कुल arguments की count 3
$* सभी arguments एक साथ (single string के रूप में) "apple banana cherry"
$@ सभी arguments अलग-अलग quoted words के रूप में "apple" "banana" "cherry"
$? Last run command का exit status (0 = success, non-zero = error) 0 (अगर command successfully चली)
$$ Current running shell script की Process ID (PID) 4289 (unique process number)
$! Background में run की गई last command की PID 4310

$* और $@ में क्या फर्क है? ($* vs $@ Difference)

University exams और technical interviews में यह सवाल बहुत पूछा जाता है। बिना double quotes के $* और $@ एक जैसा काम करते हैं। असली फर्क तब आता है जब आप उन्हें double quotes में रखते हैं:

  • "$*": सारे arguments को मिला कर एक single word बना देता है। बीच में IFS (Internal Field Separator, default space) लगता है। यानी loop सिर्फ 1 बार चलेगा।
  • "$@": हर argument को अलग-अलग independent quoted string मानता है। अगर arguments में spaces भी हों, तो भी हर argument separate entity रहता है। Loops में हमेशा "$@" use करना safe होता है।

Solved Trace Example: "$*" vs "$@" Proof

नीचे दी गई script demo_args.sh को run करके दोनों का फर्क साफ देखा जा सकता है:

#!/bin/bash
echo "Total Arguments ($#): $#"

echo "--- Loop using \"\$*\" ---"
count1=1
for arg in "$*"
do
    echo "Item $count1: $arg"
    count1=$((count1 + 1))
done

echo "--- Loop using \"\$@\" ---"
count2=1
for arg in "$@"
do
    echo "Item $count2: $arg"
    count2=$((count2 + 1))
done

अब इस script को 3 arguments देकर चलाएं: ./demo_args.sh "Data Structures" "Operating Systems" "Computer Networks"

Exact Terminal Output:

Total Arguments ($#): 3
--- Loop using "$*" ---
Item 1: Data Structures Operating Systems Computer Networks
--- Loop using "$@" ---
Item 1: Data Structures
Item 2: Operating Systems
Item 3: Computer Networks

इस output से साफ साबित होता है कि "$*" ने 3 arguments को जोड़कर 1 string बना दिया, जबकि "$@" ने तीनों को अलग-अलग preserve रखा।

Basic Components of Shell Scripting

किसी भी logic को script में code करने के लिए 4 main building blocks होते हैं:

1. Variables (User Defined)

Shell script में variable create करते समय datatype (int, float, string) declare नहीं करना पड़ता। Value assign करने के लिए = operator use होता है। Variable की value read करने के लिए नाम के आगे $ लगाना जरूरी है।

name="Rahul Sharma"
marks=85
echo "Student Name: $name"
echo "Marks Obtained: $marks"

Golden Rule: = के दोनों तरफ कोई space नहीं होना चाहिए। marks = 85 लिखने से shell error देगा।

2. User Input (read command)

Keyboard से user input लेने के लिए read command इस्तेमाल की जाती है:

echo "Enter your college name:"
read college_name
echo "Your college is: $college_name"

3. Conditional Statements (if-else)

Conditions check करने के लिए if, then, else, elif और fi use होता है। Test condition को [ ] (square brackets) के अंदर लिखा जाता है:

#!/bin/bash
echo "Enter a number:"
read num

if [ $num -gt 0 ]
then
    echo "$num is positive"
elif [ $num -lt 0 ]
then
    echo "$num is negative"
else
    echo "The number is zero"
fi

Shell script में comparison के लिए standard operators अलग होते हैं:

  • Number Comparison: -eq (equal), -ne (not equal), -gt (greater than), -lt (less than), -ge (greater or equal), -le (less or equal)
  • String Comparison: = (strings बराबर हैं), != (strings बराबर नहीं हैं), -z (string empty है)
  • File Tests: -f file (file exist करती है), -d dir (directory exist करती है), -r file (read permission है), -w file (write permission है), -x file (execute permission है)
Advertisement

4. Loops (for, while)

बार-बार task repeat करने के लिए for और while loops use होते हैं:

# for loop example: 1 to 5 numbers print करना
for i in 1 2 3 4 5
do
    echo "Number: $i"
done
# while loop example: counter check करना
count=1
while [ $count -le 3 ]
do
    echo "Count value: $count"
    count=$((count + 1))
done

Solved Practical Example: Marksheet Script with Calculation

यह एक complete, ready-to-run Bash script है जो student के 3 subjects के marks लेती है, total और percentage calculate करती है, और pass/fail result देती है:

#!/bin/bash
# File: student_result.sh

echo "=== Student Marks Calculation ==="
echo "Enter Marks for Subject 1:"
read sub1
echo "Enter Marks for Subject 2:"
read sub2
echo "Enter Marks for Subject 3:"
read sub3

# Basic validation: check if marks are non-negative
if [ $sub1 -lt 0 ] || [ $sub2 -lt 0 ] || [ $sub3 -lt 0 ]
then
    echo "Error: Marks negative नहीं हो सकते!"
    exit 1
fi

# Total calculation
total=$((sub1 + sub2 + sub3))
percentage=$((total / 3))

echo "---------------------------------"
echo "Total Marks: $total / 300"
echo "Percentage: $percentage%"

# Result determination
if [ $percentage -ge 40 ]
then
    echo "Final Result: PASS"
else
    echo "Final Result: FAIL"
fi

Script Execution और Output:

$ chmod +x student_result.sh
$ ./student_result.sh
=== Student Marks Calculation ===
Enter Marks for Subject 1:
65
Enter Marks for Subject 2:
70
Enter Marks for Subject 3:
75
---------------------------------
Total Marks: 210 / 300
Percentage: 70%
Final Result: PASS

Shell Scripting के Advantages और Disadvantages

Advantages (फायदे) Disadvantages (नुकसान)
Routine और repetitive admin tasks पूरी तरह automate हो जाते हैं Compiled languages (जैसे C, C++, Go) की तुलना में execution speed slow होती है
Syntax आसान है, compile करने की जरूरत नहीं सीधे run होती है Complex business logic और heavy calculations के लिए suitable नहीं है
Linux system commands, pipes और utilities (grep, awk, sed) का seamless integration अलग-अलग Linux/Unix distros और shell versions के बीच portability issue आ सकता है
System administrators के लिए quick setup और fast prototyping Debugging tools limited हैं; run-time errors पकड़ना मुश्किल होता है
Scheduled execution के लिए cron jobs के साथ directly configure हो जाती है Weak type checking और minimal error handling support

Shell Scripting के Real-World Uses (System Administration)

  • Automated Data Backup: हर रात database या important files का tar archive बनाना, compress करना और backup server पर transfer करना।
  • Server Resource Monitoring: Disk usage (df -h), RAM consumption (free -m), और CPU load track करना। Disk space 90% से ज्यादा होने पर system admin को automatic alert भेजना।
  • Log File Cleanup: Server पर पुरानी log files को zip करना और 30 दिन से पुराने logs को delete करके hard disk free करना।
  • User Account Management: College lab में semester की शुरुआत में 100 नए students के Linux user accounts एक single CSV file से loop चलाकर automatically create करना।
  • Cron Job Integration: Linux के crontab scheduler में script जोड़ना, जिससे बिना manual intervention के script हर रोज तय समय पर execute हो।

Common Mistakes to Avoid in Shell Scripting

  1. Variable assignment में spaces लगाना: score = 100 लिखना गलत है। Shell score को एक command समझ लेता है। हमेशा score=100 लिखें।
  2. Test brackets [ ] में spaces छोड़ना: if [$a -eq $b] लिखना syntax error देगा। Brackets के दोनों तरफ space होना जरूरी है: if [ $a -eq $b ].
  3. Variables को double quotes में न बांधना: अगर variable में spaces वाली string है (जैसे file="my report.txt"), तो rm $file लिखने पर shell दो अलग files ढूंढेगा (my और report.txt)। हमेशा "$file" लिखें।
  4. Permission denied error: Script run करने से पहले chmod +x filename.sh करना भूल जाना।
  5. Windows line endings (CRLF issue): Script को Windows Notepad में लिख कर Linux पर run करने पर /bin/bash^M: bad interpreter error आता है। हमेशा Linux text editor use करें या dos2unix चलाएं।

Exam में कैसे लिखें (5 marks)

अगर university exam में 5 marks का question आए "Shell Scripting क्या है? इसके steps और advantages लिखिए", तो answer point-to-point इस तरह लिखें:

  • Definition: Shell script एक executable text file है जिसमें Linux commands एक sequence में लिखी होती हैं, जिन्हें Shell interpreter run करता है।
  • File Extension & Shebang: इसे .sh extension से save करते हैं। पहली line Shebang होती है: #!/bin/bash.
  • Steps to Run:
    1. File बनाएं: nano script.sh
    2. Code लिखें और save करें।
    3. Permission दें: chmod +x script.sh
    4. Run करें: ./script.sh
  • Key Components: Variables (name=val), Input (read), Output (echo), Conditions (if-else-fi), Loops (for, while).
  • Special Variables: $0 (script name), $# (arguments count), $* (all args together), $? (exit status).
  • Two Main Advantages: Repetitive tasks का automation, और Linux commands का direct integration.

Exam के लिए Important Questions

Short Questions (2–3 marks)

  1. Shebang क्या है और इसका क्या role है? (Script की पहली line #!/bin/bash, जो OS को बताती है कि script को कौन सा interpreter run करेगा)
  2. $# और $? special variables क्या represent करते हैं? ($# = total arguments की संख्या, $? = last command का exit status)
  3. "$*" और "$@" में मुख्य अंतर क्या है? ("$*" सभी arguments को एक single string मानता है, जबकि "$@" हर argument को separate string रखता है)
  4. Shell script को executable बनाने के लिए कौन सी command use होती है? (chmod +x scriptname.sh)
  5. Bash shell का full form क्या है? (Bourne Again Shell)

Long Questions (5–10 marks)

  1. Shell Scripting से आप क्या समझते हैं? Linux में shell script create, compile/execute करने के steps और special variables detail में समझाइए।
  2. Types of shells in Unix/Linux कौन-कौन से हैं? sh, bash, ksh और csh के बीच comparison table बनाइए।
  3. Command-line arguments क्या हैं? Shell script में positional parameters ($0 to $9, $#, $*, $@) को example program और output के साथ समझाइए।
  4. Shell scripting में decision making (if-else) और loops (for, while) के syntax को practical code examples के साथ समझाइए।

FAQs

$ in shell script का क्या मतलब है?

Shell script में $ symbol किसी variable की value को extract (access) करने के लिए इस्तेमाल होता है। जैसे count=5 assign करने के बाद उसे print करने के लिए echo $count लिखा जाता है। इसके अलावा special variables (जैसे $#, $*, $?) के आगे भी $ लगता है।

$* in shell script का क्या use है?

$* script को pass किए गए सभी command-line arguments को एक साथ represent करता है। जब इसे double quotes ("$*") में लिखा जाता है, तो यह सारे arguments को एक single string मान लेता है।

$# in shell script क्या बताता है?

$# script को pass किए गए positional arguments की total count देता है। अगर user ने ./test.sh a b c चलाया, तो $# की value 3 होगी।

Linux में default shell कौन सा होता है?

Ubuntu, Fedora, RedHat और अधिकतर modern Linux operating systems में default shell Bash (/bin/bash) होता है। आप echo $SHELL command चलाकर अपना current shell check कर सकते हैं।

Shell script में exit status 0 का क्या मतलब है?

Linux में जब कोई command या script बिना किसी error के successfully complete होती है, तो उसका exit code 0 होता है। कोई भी non-zero code (जैसे 1, 2, 127) यह दर्शाता है कि execution में error आया है। इसे echo $? से check किया जाता है।

अक्सर पूछे जाने वाले प्रश्न (FAQs)

Quick Answer: Shell Script एक plain text file होती है जिसमें Linux/Unix की commands एक sequence में लिखी होती हैं, जिन्हें Shell interpreter एक-एक करके run करता है।
Operating System (OS) Notes in Hindi | ऑपरेटिंग सिस्टम नोट्स के पाठ्यक्रम में Shell Script in Hindi एक महत्वपूर्ण अध्याय है। यह मुख्य सिद्धांतों, परीक्षा प्रश्नों और सूत्रों को समझने के लिए अत्यधिक आवश्यक है।
हाँ, NotesMedia पर आपको Shell Script in Hindi के महत्वपूर्ण प्रश्नोत्तर, हल किए गए उदाहरण और नोट्स बिल्कुल मुफ्त मिलेंगे। अधिक अभ्यास के लिए आप हमारा ऐप भी डाउनलोड कर सकते हैं।
Advertisement

In this Chapter

Shell Script in Hindi
Distributed System in Hindi
Real Time System in Hindi
System Calls in Hindi
System Program in Hindi
Structure of Operating System in Hindi
Layers Design of Structure in Hindi
Unix in Hindi
Virtual Machine in Hindi
Kernel-Based Operating System in Hindi
Process in Hindi – OS में Process क्या है
Interacting Processes in Hindi
Threads in Hindi
Fundamental of Schedulling in Hindi
Scheduling Criteria in Hindi
Long Medium Short Term Scheduling in Hindi
Scheduling Algorithms in Hindi
Structure of Concurrent System in Hindi
Critical Region in Hindi
Critical Section in Hindi
Inter process Communication in Hindi
Monitor in Hindi
Semaphore in OS in Hindi – Counting और Binary Semaphore
Semaphores Implementation & Uses in Hindi
Logical and Physical Address in Hindi
Swapping in Hindi
Contiguous Allocation in Hindi
Segmentation in Hindi
Paging in OS in Hindi – Paging और Segmentation
Paging with Segmentation in Hindi
kernel Memory Allocation in Hindi
Page Replacement Algorithms in Hindi
Virtual Memory in Hindi
Virtual Memory with demand Paging in Hindi
Deadlock & Characterization in Hindi
Methods for Handling Deadlocks in Hindi
Deadlock Thrashing in Hindi
Allocation of Frames in Hindi
Virtual Memory using Segmentation in Hindi
Interprocess Communication protocol in Hindi
Network Operating System in Hindi
Design Issues in Distributed File System in Hindi
Network Structure in Hindi
Structure Distributed System in Hindi
File System and Coordination in Hindi
Linux in Hindi
Commands of Linux in Hindi | लिनक्स के कमांड्स हिंदी में
Programmer Interface & File manipulation in Hindi
Process control in Linux in Hindi
Linux Kernel & Signals in Hindi
File System in Linux in Hindi
block & inodes in Hindi
System Editor in Hindi
Character Transliteration in Hindi
ED in Hindi
Vi Editor & There Commands in Hindi
Looping & Decision Making in Hindi
Variables & File name Expansion in Hindi
Array in Hindi OS
Sub Program in Hindi
C interface with Linux in Hindi
Simpel Shell Programs in Hindi
BCA Part-1 | Operating Systems | Semester-I | MDSU Exam Paper 2023 (Held in 2024)
What is Operating System (OS) in Hindi
Operating System (OS) All Important Questions and Answers in Hindi (MDSU)
BCA || Operating System 2025 Paper || MDSU Exam Paper
BCA | OS(Operating System) 2023 Paper | MDSU Exam Paper
Type of Operating System in Hindi
Goals of Operating System in Hindi
Operation of Operating system in Hindi
Resource Allocation & Functions in Hindi
class of Operating system in Hindi
Batch Processing in Hindi
Multi Processing in Hindi
Time Sharing in Hindi