---
title: "Short note: shell scripts"
subject: "Operating System"
chapter: 9
key: "semester_6/operating_system:shell-scripts-note"
source: https://www.bctengineer.com/q/semester_6/operating_system/shell-scripts-note
site: BCT Engineering
---

# Short note: shell scripts

Operating System · Chapter 9: System Administration

- Asked in: 2070 Bhadra, 4 marks; 2071 Magh, 3 marks (asked 2 times)
- Video: [What is shell | what is shell scripting | scripting | bash scripting | type of shell in linux](https://www.youtube.com/watch?v=ROB3OzcWA6w) — Everything Technical
- Answer checked by Claude Opus 5.5 (high effort) on 2026-10-14
- Page: https://www.bctengineer.com/q/semester_6/operating_system/shell-scripts-note
- Whole chapter: https://www.bctengineer.com/md/semester_6/operating_system/chapter-9

## Question

Write a short note on shell scripts.

## Short answer

A shell script is a text file of commands executed by a shell to automate tasks. It uses variables, conditionals, loops, and functions. Common uses include backups, log cleanup, and cron jobs. Simple and portable but slower than compiled programs.

## Answer

A **shell script** is a text file containing a sequence of shell commands and control statements, which the shell (sh, bash, ksh) interprets and executes line by line. It is used to automate repetitive tasks, especially in system administration.

#### Features
- Variables, input (`read`), arithmetic.
- Control structures: `if`, `case`, `for`, `while`.
- Functions, command-line arguments (`$1`, `$2`, `$#`), exit status `$?`.
- Pipes, redirection and wildcards.
- Interpreted: no compilation.

#### Example
```bash
#!/bin/bash
# backup home directory and delete old backups
DATE=$(date +%F)
tar -czf /backup/home-$DATE.tar.gz /home
if [ $? -eq 0 ]; then
    echo "Backup done on $DATE"
fi
find /backup -mtime +7 -delete
```
Make it executable with `chmod +x backup.sh` and run `./backup.sh`.

#### Uses
- Automating backup, log cleaning, user creation.
- Running at set times with `cron`.
- Start-up and shutdown scripts at boot.
- Batch processing of files.

Advantages: simple, quick to write, portable. Disadvantages: slower than compiled programs, and poor for complex programs.

## Check yourself

The answer's key points as multiple-choice questions, with the correct option and why.

1. What is a shell script?
   A. A compiled binary produced from C code
   B. A text file of shell commands and control statements interpreted line by line by the shell
   C. A hardware device driver
   D. A type of file system
   Answer: B. A shell script is a text file of commands and control statements that the shell interprets and executes line by line, without compilation. (Key point: What a shell script is)

2. In the backup example, what does the test if [ $? -eq 0 ] check?
   A. Whether the script has zero arguments
   B. Whether the previous command (tar) finished successfully
   C. Whether the date is correct
   D. Whether the file is executable
   Answer: B. $? holds the exit status of the last command, and 0 means success, so the message is printed only if the backup succeeded. (Key point: Exit status)

3. What must be done before running ./backup.sh directly?
   A. Compile it with gcc
   B. Make it executable with chmod +x backup.sh
   C. Copy it into /boot
   D. Rename it to .exe
   Answer: B. The script needs execute permission, given by chmod +x, because it is interpreted, not compiled. (Key point: Running a script)

4. Which is a disadvantage of shell scripts compared with compiled programs?
   A. They need a long compilation step
   B. They are slower and poor for complex programs
   C. They cannot use loops
   D. They cannot be scheduled with cron
   Answer: B. Being interpreted, shell scripts run slower and are unsuitable for complex programs, though they are simple, quick to write and portable. (Key point: Advantages and disadvantages)
