Skip to content
geeksforgeeks
  • Tutorials
    • Python
    • Java
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
    • Practice Coding Problems
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • Shell Scripting
  • Kali Linux
  • Ubuntu
  • Red Hat
  • CentOS
  • Docker in Linux
  • Kubernetes in Linux
  • Linux interview question
  • Python
  • R
  • Java
  • C
  • C++
  • JavaScript
  • DSA
Open In App
Next Article:
Bash Scripting - Working of Bash Scripting
Next article icon

Bash Scripting - Working of Bash Scripting

Last Updated : 30 Dec, 2022
Comments
Improve
Suggest changes
Like Article
Like
Report

Bash Scripting is a crucial component of Linux's process automation. You can write a series of commands in a file and then execute them using scripting. A Bash script is a plain text file with a set of commands inside of it. These commands are a combination of ones we typically type on the command line ourselves (like ls or cp, for instance) and others that we could type on the command line but typically wouldn't. Any command that can be executed regularly on the command line can be put into a script and will execute exactly as intended. Likewise, whatever you can include in a script can also be executed regularly on the command line and will provide the same results. You can save time by not having to repeatedly type specific commands as a result. Daily duties can be completed effectively and even scheduled for automated completion. Additionally, you can configure some scripts to run at startup, such as those that display a specific message when a new session is started or modify the environment.

Bash Scripting and Bash Shell

checking bash version
 

Bash shell is a software application known as the shell that offers Linux command line management. The shell program has changed through the years to accommodate different options. Bash is powerful because it can streamline tasks that are challenging to complete effectively using a GUI. Keep in mind that not all servers offer a graphical user interface (GUI), thus it is best to learn how to use a command line interface (CLI).

When a shell is used interactively, it will show information, like username, host, and a $ when it is awaiting a command from the user. This is known as the shell prompt.

[username@host ~]$

If the shell is running as the root user, the prompt will end with the # symbol.

[root@host ~]#

It is possible to set up different users to use various shells. But the majority of users choose to continue using the current standard shell. The GNU Bourne-Again Shell is the standard shell for many Linux distributions (bash). Bourne Shell succeeds Bash (sh). The startup script used by the shell when it is first launched may be found in the .bashrc or .bash profile file and allows you to modify how the shell behaves.

bash-config-file-in-user-home-directory
 

You can use nano to edit the add your script in bashrc config to load in a terminal startup. 

bashrc-edit-in-nano-editor
 

We write the bash script which is work on the bash shell, so we can use another shell but we should switch to the bash shell instead for running the bash scripts. The startup script written in bashrc may face conflict with other shells or will not be loaded in another shell. 

Working of Bash Script

We have the idea of applications and processes in Linux. A program is a blob of binary data that is normally saved on your hard drive and consists of a set of instructions for the CPU as well as additional resources (such as graphics and sound files). When we say we are running a program, we are really executing a process - a duplicate of the program. This information and instructions are copied from the hard drive into RAM. A small amount of RAM is also set aside for the process to store variables (which will hold temporary working data) and a few flags to let the operating system control and monitor the process while it runs. The same software may be executed in memory in multiple instances as different processes. For instance, I might be performing the command cp in each of the two open terminals. There would be two cp processes running on the machine in this scenario. Once they have completed operating, the system eliminates them, and the program cp is no longer represented by any processes. A Bash process is running at the terminal to provide us with the Bash shell. When we start a bash script, it doesn't actually run in that process; instead, it creates a new one inside which it can run. This will be demonstrated in the section on variables that follows, and its ramifications should be made obvious. However, you shouldn't worry too much about this phenomenon in general.

Identify a Bash Script file

However, they can function just fine without the sh extension. When an edit the file header or first line generally starts with bash bang. Bash bang basically consists of the directory of the bash shell program to be used for script execution. The hash exclamation mark #! character sequence is referred to as the Shebang. It basically uses to set the path of the bash program. The bash bang looks like the following structure

#! /bin/bash

or 

#! /usr/bin/bash

Create and Run a simple Bash Script

To create a bash script open the terminal and create a file with the .sh extension and include the bash bang path, after this write the command which you generally run in the shell.

To create a file you can use GUI or in the terminal using nano or Vim editor to write the scripts

$ nano hello.sh
bash-nano-file-editor
 

To execute the program you can the following command. 

$ bash hello.sh  hello world

or 

$ ./hello.sh  hello world

If you directly create a file with the proper structure using nano or vim, by default the file will be executable and if you run the ls command, you can see the fill color will be green, it shows that the file is executable, but if you do not follow that, the file will not be executable by default, you can make the file executable to run using ./ operator.

 running-bash-script
 
$ chmod +x hello.sh  $ ./hello.sh  hello world

Conclusion

Bash Scripting is just like another programming language which is deal with OS and kernels, bash specifically deal with Linux OS and make the work process easier to manage by writing short instructions for a specific task, a Large number of a server runs on Linux, where we perform various CI/CD pipeline operation where we have to build various application and task operation where bash scripts just make it a smooth process and save lots of time and there are lot more utilities of bash script like we can write a script for setup a system, build an application template in Linux and lot more.


Next Article
Bash Scripting - Working of Bash Scripting

R

ravi0900
Improve
Article Tags :
  • Technical Scripter
  • Linux-Unix
  • Technical Scripter 2022

Similar Reads

    Bash Scripting - Working of Alias
    BASH Alias is a shortcut to run commands using some mapping. It is a way to create some shortcut commands for repetitive and multiple tasks. We create an alias in a BASH environment in specified files. We can also incorporate BASH functions, variables, etc to make the Alias more programmatic and fle
    7 min read
    Bash Script - Working of Bash Variables
    A Bash script is a plain text file. This file contains different commands for step-by-step execution. These commands can be written directly into the command line but from a reusability perceptive it is useful to store all of the inter-related commands for a specific task in a single file. We can us
    7 min read
    Bash Scripting - How to Run Bash Scripting in Terminal
    In this article, we are going to see how to run bash script in terminal. For example, we create a bash file set of instructions or commands ( known as bash script ), and through command, we can run this file in terminal. Let see how to run bash script in terminal. Example 1 : In this example we prin
    2 min read
    Bash Scripting - Introduction to Bash and Bash Scripting
    Bash is a command-line interpreter or Unix Shell and it is widely used in GNU/Linux Operating System. It is written by Brian Jhan Fox. It is used as a default login shell for most Linux distributions. Scripting is used to automate the execution of the tasks so that humans do not need to perform them
    12 min read
    Bash Scripting - How to Initialize a String
    Strings are quite crucial aspects of programming and scripting. We need to store the characters in some variables. In this article, we'll see how to initialize strings in BASH. Basic String Initialization To initialize a string, you directly start with the name of the variable followed by the assign
    2 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences