[Day 5] 3. Shell Scripting or Shell Programming
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
.sh1. 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 command.
chmod - 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
echo- By default: outputs a newline character at the end.
echo - To avoid newlines: Use the option (e.g.,
-n).echo -n - Escape sequences: Don’t work by default in . Use the
echooption to enable them.-e
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 command.
env
Example:
- : Specifies directories where executable programs are located.
PATH
!3. Env.png
6. man
Command
manThe
man- 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
printfman 3 printf7. 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
expr- 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
bc- The command (basic calculator) is used for more advanced arithmetic, including floats.
bc - Redirection can be used with to output calculations to a file or standard output.
bc
Example:
echo 1.5 + 1.5 | bc3.09. Command Substitution
-
Command substitution replaces a command with its output.
Two ways to use it:
$(command)`command`
10. Conditional Statements in Shell Scripting
if
Statements:
ifif [ condition ] then ... elif [ condition ] then ... else ... fi
- Note: Indentation is optional, but recommended for readability.
case
Statements:
casecase $ in 1|4) ... ;; 2) ... ;; *) ... ;; esac
- : Acts as the default case.
* - Cases can include integers, floats, or strings (e.g., "one", "two").
Read Next
[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...
[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...