What Is a Script?
A script is:
A text file that contains a list of commands.
When you run the script, the system executes each command in order.
Think of it like:
Instead of telling someone step-by-step what to do every time, you write instructions once and hand them over whenever needed.
Why Use Scripting?
Scripting helps you:
Example tasks:
Basic Bash Script (Linux/macOS)
On Linux or macOS, scripting is often done using Bash.
Create a file called:
myscript.sh
Example:
#!/bin/bash echo "Hello, world!" echo "This is my first script." mkdir TestFolder cd TestFolder touch example.txt echo "Script finished."
Explanation:
#!/bin/bash tells the system to use Bash.echo prints text.mkdir creates a folder.cd changes directory.touch creates a file.To make it executable:
chmod +x myscript.sh
To run it:
./myscript.sh
Basic Batch Script (Windows)
On Windows, basic scripting is often done using batch files.
Create a file called:
myscript.bat
Example:
@echo off echo Hello, world! echo This is my first script. mkdir TestFolder cd TestFolder type nul > example.txt echo Script finished. pause
Explanation:
@echo off hides command repetition.echo prints text.mkdir creates a folder.cd changes directory.type nul > file.txt creates a file.pause waits for user input.To run it:
Double-click the file
or run from Command Prompt:
myscript.bat
Variables in Scripts
Scripts can store information using variables.
Bash Example
#!/bin/bash name="John" echo "Hello, $name"
Windows Batch Example
@echo off set name=John echo Hello, %name% pause
Variables allow your script to become dynamic.
Simple User Input
Bash
#!/bin/bash echo "Enter your name:" read name echo "Hello, $name"
Windows Batch
@echo off set /p name=Enter your name: echo Hello, %name% pause
Now the script interacts with the user.
Basic Conditional Logic
Scripts can make decisions.
Bash
#!/bin/bash
number=10
if [ $number -gt 5 ]; then
echo "Number is greater than 5"
fi
Windows Batch
@echo off
set number=10
if %number% GTR 5 (
echo Number is greater than 5
)
pause
This allows scripts to behave differently depending on conditions.
Basic Loop Example
Loops repeat commands.
Bash
#!/bin/bash
for i in 1 2 3
do
echo "Number $i"
done
Windows Batch
@echo off
for %%i in (1 2 3) do (
echo Number %%i
)
pause
How Scripts Work Internally
When you run a script:
A script is not compiled like a traditional program.
It is interpreted line by line.
# Bash Script Example
#!/bin/bash
echo "Hello, world!"
echo "This is my first script."
mkdir TestFolder
cd TestFolder
touch example.txt
echo "Script finished."
# Make executable
chmod +x myscript.sh
./myscript.sh
# Bash Variable Example
name="John"
echo "Hello, $name"
# Bash Input Example
echo "Enter your name:"
read name
echo "Hello, $name"
# Bash Conditional
number=10
if [ $number -gt 5 ]; then
echo "Number is greater than 5"
fi
# Bash Loop
for i in 1 2 3
do
echo "Number $i"
done
@echo off
echo Hello, world!
echo This is my first script.
mkdir TestFolder
cd TestFolder
type nul > example.txt
echo Script finished.
pause
set name=John
echo Hello, %name%
pause
set /p name=Enter your name:
echo Hello, %name%
pause
set number=10
if %number% GTR 5 (
echo Number is greater than 5
)
pause
for %%i in (1 2 3) do (
echo Number %%i
)
pause