DEV Community

Cover image for Bash Shell Scripting
Frank
Frank

Posted on

Bash Shell Scripting

Easily understood, the shell is a software that accepts keyboard commands and sends them to the operating system for execution. Previously, it was the only user interface available on Unix-like systems such as Linux. Nowadays, we have both graphical user interfaces (GUIs) and command line interfaces (CLIs), such as the shell.

The shell application on most Linux systems is called bash (Bourne Again Shell, an expanded version of Steve Bourne's original Unix shell program, sh). In addition to bash, various shell programs are available for Linux systems. These include ksh, tcsh, and zsh.

  1. Introduction
  2. What is a Terminal?
  3. Navigating in the Terminal

Introduction

Bash Shell scripting is a powerful way to automate tasks and manage your system efficiently. This guide will walk you through the basics of Bash scripting using Git Bash, making it easy to understand even if you're new to the command line.

What is a Terminal?

Understanding the Terminal

It's a program called a terminal emulator. This is a program that opens a window and lets you interact with the shell.
A terminal is a text-based interface that enables users to interact with the operating system, execute commands, run programs, and manage files without requiring a graphical user interface (GUI). Unlike graphical user interfaces (GUIs), the terminal requires you to type commands to perform tasks, making it a powerful tool for those who learn how to use it.

A command line interface

A command line interface

 A graphical user interface

A graphical user interface

Why Use Git Bash?

Git Bash is a terminal emulator for Windows that provides a Bash command-line experience. It allows you to use Git commands and Bash scripting on a Windows operating system, bridging the gap between Windows and Unix/Linux environments.


Let's Start Our Terminal
We'll be using Git bash as our terminal.
Check this video out to install git bash on your PC.. After installation, right-click on your mouse and click on Open Git Bash here. You should see Something like this:

Image description

Okay, let's attempt some typing. Opening a terminal window. The first thing we should see is the USER(which represents the username on the system): @DESKTOP-2H295Q8 (Your computer's hostname is a unique identifier on the network that uniquely identifies it.), MINGW64(this indicates that you are using a 64-bit version of Git Bash), ~/Desktop(The ~ symbol represents your home directory, and ~/Desktop means you are currently located in the "Desktop" directory within your home directory.) Something like this:

Image description

Excellent! Now, type some gibberish characters and press the Enter key.

Gibberrish

We should receive an error message stating that it could not understand the command.

Image description

Wonderful! Now press the up arrow key. Watch how the preceding command ghdghkdhfoihsgkjndjkgnkj responds. Yes, we have a command history. When we press the down arrow, we return to the blank line.
Recall the ghdghkdhfoihsgkjndjkgnkj command using the up-arrow key if necessary, and then try the left and right arrow keys. The text cursor can be moved anywhere on the command line, making it simple to repair errors.


Using The Mouse
Even though the shell is a command-line interface, the mouse remains useful.

Aside from scrolling the contents of the terminal window, the mouse can also be used to copy text. Drag the mouse over some text (for example, ghdghkdhfoihsgkjndjkgnkj right here in the browser window) while holding down the left button. The text should be highlighted. Release the left button and move the mouse pointer to the terminal window, then hit the middle mouse button (or, if using a touch pad, press both the left and right buttons simultaneously). The highlighted content in the browser window should be copied to the command line.

Navigating in the Terminal

Those unfamiliar with the command line should pay close attention to this session because the principles will take some time to grasp.

Like Windows, files on a Linux system are organized in a hierarchical directory structure. This means that they are structured in a tree-like structure of directories (known as folders in other systems), which may include files and subdirectories. The root directory refers to the initial directory in the file system. The root directory contains files and subdirectories that contain other files and subdirectories, and so on.
Most graphical environments contain a file manager tool that allows you to view and control the contents of the file system. We often see the file system illustrated like this.

Image description

Learning to navigate the terminal is like learning to navigate a new city. Here are some basic commands to get you started:

Basic Commands

  • pwd (Print Working Directory): Shows the current directory you're in.

Image description

  • ls: Lists the files and directories in your current location.

Image description

  • cd: Changes the directory. To change our working directory, we use the cd command. To do this, type cd followed by the pathname to the desired working directory. A pathname is the route we take through the tree's branches to get to the desired directory. Pathnames can be defined in two ways: as absolute pathnames and as relative pathnames. Let's start with absolute pathnames.

An absolute pathname starts with the root directory and progresses along the tree, branch by branch, until the path to the target directory or file is complete. For example, most documents are saved in a certain directory on your system. The directory's pathname is /Onedrive/Documents. This indicates that from the root directory (indicated by the leading slash in the pathname), there is a directory called "Onedrive" that contains another directory called "Documents".
Let's check it out:

Image description
We can now see that we have moved the current working directory to /OneDrive/Documents, which is full of files. Notice how the shell prompt has changed? It is typically configured to display the name of the current working directory for convenience.

An absolute pathname begins in the root directory and leads to its destination, whereas a relative pathname begins in the working directory. To accomplish this, it employs a few unique notations to denote relative positions in the file system tree. These special notations include "." (dot) and ".." (dot dot).

The "." notation refers to the working directory itself, while the ".." notation refers to its parent directory. Here's how it works. Let's change the working directory to /OneDrive/Documents again.

Image description

So, let's imagine we wanted to shift the working directory to /OneDrive/Documents' parent, which is /OneDrive. We could achieve this in two distinct ways. Starting with an absolute pathname:

Image description

Or, using a relative pathname:

Image description

Two different approaches yielded identical results. Which should we use? The one you think involves the least amount of typing!
Similarly, we can move the working directory from /OneDrive to /OneDrive/Documents in two distinct ways. First, use an absolute pathname:

Image description

Or, using a relative pathname:

Image description

You need to know there is something crucial to note here. In most cases, we may take off the "./". It's suggested. Typing:

Image description
would perform the same thing. Generally, if we do not provide a pathname to something, the working directory is assumed.

More Navigation Commands In Bash

  • mkdir(Make Directory)
    Create a new directory: Create a new folder in the current location.

     mkdir new_folder
    
  • rmdir (Remove Directory)
    Remove an empty directory: Delete a folder that does not contain any files.

     rmdir old_folder
    
  • rm (Remove)
    Remove a file: Delete a file in the current directory.

     rm file.txt
    

    Remove a directory and its contents: Deletes folder & all in it.

     rm -r folder_name
    
  • cp (Copy)
    Copy a file: Copy a file from one location to another.

     cp source.txt destination.txt
    

    Copy a directory: Copy a directory and its contents.

     cp -r source_folder/ destination_folder/
    
  • mv (Move or Rename)
    Move a file or directory: Move a file or directory to a different location.

     mv file.txt /path/to/destination/
    

Rename a file or directory: Change the name of a directory/file.

 mv old_name.txt new_name.txt
Enter fullscreen mode Exit fullscreen mode
  • touch
    Create an empty file: Create a new, empty file or update the timestamp of an existing file.

     touch newfile.txt
    
  • cat (Concatenate)
    View the contents of a file: Display the contents of a file directly in the terminal.

     cat file.txt
    
  • less
    View file content page by page: Open a file for reading one page at a time.

     less file.txt
    
    • Use q to quit when done.
  • find
    Search for files and directories: Find files or directories by name.

     find . -name "filename.txt"
    
    • Search in a specific directory:
     find /path/to/search -name "filename.txt"
    
  • grep
    Search inside files: Find a specific string within files.

     grep "search_term" file.txt
    
    • Search recursively in all files within a directory:
     grep -r "search_term" /path/to/directory
    

These commands form the backbone of file system navigation in Bash. Understanding and practicing them will greatly enhance your efficiency when working in the terminal.

Top comments (2)

Collapse
 
dev_frank profile image
Frank

Hello everyone!🤚
I've just published a Bash Shell scripting guide to help you get started with command-line automation. Whether you're new to scripting or simply want to brush up on your skills, I believe you'll find it useful.
🚀 Please check it out and don't hesitate to contact me if you have any questions—I'm happy to help!

Collapse
 
cavo789 profile image
Christophe Avonture

Oh just go to the next level : enable WSL2 (Windows Subsytem for Linux) and install a real Linux distribution like Ubuntu or Debian. It's really easy to do and you'll a full Linux environnement without leaving Windows (yeah that's sounds, it's, wonderful).

Stop faking Linux using Git Bash.