Bash Scripting | Complete Introduction For BCA | MCA | BSC-IT | Full Course
Are you ready to dive into the world of Bash scripting? Whether you're a beginner or looking to enhance your scripting skills, this comprehensive guide will help you learn Bash scripting in a simple way. Bash, or Bourne Again Shell, is a powerful command language interpreter used in Unix/Linux environments. It allows you to automate tasks, manage files, and perform various operations efficiently. Let's get started!
Introduction to Bash Scripting
What is Bash Scripting?
Why Learn Bash Scripting?
- Automation: You can write scripts to automate repetitive tasks. For example, instead of manually backing up files every day, you can write a script to do it for you.
- Efficiency: With Bash scripts, you can perform complex tasks with simple commands, saving time and effort.
- System Management: It’s useful for managing system tasks like creating directories, copying files, and installing software.
- Customization: You can create custom workflows and tools that fit your specific needs.
Basic Bash Command Overview
These are some basic commands to get you started:
- ls: Lists the files and directories in the current directory. This command shows you what's inside the folder you are currently in.
- cd: Changes the current directory. Use this command to move into a different folder.
- pwd: Prints the current working directory. This command tells you where you are in the file system.
- echo: Displays a line of text. Use this command to print text to the screen.
- man: Displays the manual for a command. This command shows detailed information about how to use other commands.
Setting Up the Environment for Bash Scripting
To get started with Bash scripting, you need to set up your environment. This involves installing Bash, choosing a code editor, and learning basic command line navigation.
Here’s a simple guide for beginners:
Installing Bash on Various Operating Systems
1. How to Install Bash on Linux
To install Bash on Linux first, you check whether it is already installed or not because Most Linux distributions come with Bash pre-installed. To check if Bash is installed, open your terminal and type:
bash --version
You should see the version of Bash installed on your system. If Bash is not installed, you can install it using your package manager. For example, on Ubuntu, you can use:
2. How to Install Bash on MacOS
To install Bash on Linux first, you check whether it is already installed or not because Bash comes pre-installed on macOS. To check the version, open the Terminal app and type:
bash --version
If you need to update Bash, you can use Homebrew, a package manager for macOS. First, install Homebrew if you haven’t already:
/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"
Then, update Bash:
brew install bash
How to install Bash on Windows?
Windows does not natively support Bash, but you can install it using the Windows Subsystem for Linux (WSL). Here’s how:
Step 1: Enable WSL: Open PowerShell as an administrator and run:
brew --install
Step 2: Restart your computer: After enabling WSL, restart your computer.
Step 3: Install a Linux distribution: Open the Microsoft Store, search for "Ubuntu", and install it.
Step 4: Set up Ubuntu: Open Ubuntu from the Start menu, follow the on-screen instructions to set it up, and you’ll have access to Bash.
Setting Up a Code Editor
A good code editor makes writing and editing Bash scripts easier. Here are some popular choices:
1. VSCode (Visual Studio Code):
- Download and install from Visual Studio Code.
- Install the Bash extension for syntax highlighting and other features.
2. Sublime Text:
- Download and install from Sublime Text.
- Install the Bash syntax highlighting package.
3. Atom:
- Download and install from Atom.
- Install the language-bash package for Bash support.
Basic Command Line Navigation
Introduction to the Terminal
The terminal, also known as the command line or console, is a text-based interface used to interact with your computer. Unlike the graphical user interface (GUI) where you click on icons and buttons, the terminal allows you to type commands to perform various tasks.
Why Use the Terminal?
- Efficiency: Commands can often be faster than navigating through menus.
- Control: Provides more control and options than graphical interfaces.
- Scripting: Allows automation of repetitive tasks through scripts.
Opening the Terminal
-
Linux: Look for "Terminal" in your applications menu or
press
Ctrl + Alt + T
. -
macOS: Open "Terminal" from the Applications > Utilities
folder or press
Command + Space
and type "Terminal". - Windows: Use "Command Prompt" or "PowerShell" by searching for them in the Start menu.
Basic Commands
Here are some basic commands to get you started:
-
ls
: Lists files and directories in the current directory. -
Example: Type
ls
and press Enter. You'll see a list of files and folders in your current location. -
cd
: Changes the current directory. -
Example: Type
cd Documents
and press Enter to move into the "Documents" directory. -
To go back to the previous directory, type
cd ..
and press Enter. -
pwd
: Prints the current working directory, showing where you are in -
Example: Type
pwd
and press Enter. You'll see the path to your current directory. -
mkdir
: Creates a new directory. -
Example: Type
mkdir MyFolder
and press Enter. A new folder named "MyFolder" will be created. -
rmdir
: Removes a directory. -
Example: Type
rmdir MyFolder
and press Enter. The "MyFolder" directory will be deleted if it is empty.
Understanding File Paths
File paths tell the terminal where to find files and directories. There are two types of paths:
- Absolute Path: Starts from the root directory and includes the full path.
- Relative Path: Starts from the current directory.
Absolute Path
An absolute path is the full address of a file or folder, starting from the root directory. It provides a complete path that specifies all the directories leading to the file.
-
Root Directory: The topmost directory in the file system.
On Linux and macOS, it is represented by a single forward slash
/
. On Windows, it is represented by the drive letter followed by a colon and a backslash, likeC:\
. - Example:
- On Linux/macOS:
/home/user/documents/report.txt
- On Windows:
C:\Users\User\Documents\report.txt
In these examples, the path starts from the root directory (/
or C:\
) and follows the hierarchy down to the specific file
report.txt
.
Relative Path
A relative path specifies the location of a file or folder relative to the current directory. It does not start from the root directory but from the current working directory.
-
Current Directory: The directory you are currently
working in. It is represented by a single dot
.
.
Example:
- Current directory:
/home/user
-
Relative path to
report.txt
indocuments
folder:documents/report.txt
If you are in the /home/user
directory, the relative path
documents/report.txt
points to the same file as the absolute
path /home/user/documents/report.txt
.
Bash Commands in Bash with Options
ls
(List Directory Contents)
- Purpose: To view the contents of a directory (folder).
-
Usage: Simply type
ls
and press Enter. - Options:
ls -l
: Lists files with detailed information.-
ls -a
: Shows hidden files (files starting with a dot). -
ls -lh
: Shows detailed information with file sizes in human-readable format.
cd
(Change Directory)
- Purpose: To navigate between directories.
-
Usage: Type
cd
followed by the path to the directory you want to enter. - Example: cd Documents this command will change the current directory to the "Documents" directory
- Moving Up a Directory: cd.. This command will move you up one level to the parent directory.
- Moving to Home Directory: cd~ This command will take you to your home directory.
pwd
(Print Working Directory)
- Purpose: To display the current directory you are in.
-
Usage: Simply type
pwd
and press Enter. -
Example:
pwd
This command will show the full path of the current directory.
mkdir
(Make Directory)
- Purpose: To create a new directory.
-
Usage: Type
mkdir
followed by the name of the directory you want to create. - Example: mkdir new folder This command will create a new directory named "new_folder" in the current directory.
rmdir
(Remove Directory)
- Purpose: To delete an empty directory.
-
Usage: Type
rmdir
followed by the name of the directory you want to delete. - Example: rmdir old_folder This command will delete the directory named "old_folder" if it is empty.
-
Note: If the directory is not empty,
rmdir
will not work. You can userm -r
to remove a directory and its contents.
Summary
-
ls
: Lists directory contents. -
cd
: Changes the current directory. -
pwd
: Prints the current directory. -
mkdir
: Creates a new directory. -
rmdir
: Removes an empty directory.
These commands are fundamental for navigating and managing directories and files in Bash. Practice using them to become comfortable with navigating the file system from the command line.
Writing Your First Bash Script
Creating and Running a Simple Script
Step 1: Create a New File
- Open your terminal. The terminal is a command line interface that allows you to interact with your computer using text commands.
-
Use a text editor to create a new file. A text editor is a program that lets
you write and edit text. Some common text editors are
nano
,vim
, andgedit
. -
For example, to create a new file named
first_script.sh
usingnano
, type: nano first_script.sh -
This command opens the
nano
text editor with a new file namedfirst_script.sh
.
Step 2: Write Your Script
- In the text editor, type
echo "Hello, world!"
-
The
echo
command prints text to the screen. In this case, it will print "Hello, World!". -
Save the file and exit the editor. In
nano
, you do this by pressingCtrl+X
, thenY
to confirm you want to save, andEnter
to finalize.
Step 3: Run Your Script
-
To run the script, use the
bash
a command followed by the script’s name: bash first_script.sh - This command tells the computer to use the Bash shell to execute the
commands in the
first_script.sh
file. - You should see the output: Hello, world