EDUCBA

EDUCBA

MENUMENU
  • Free Tutorials
  • Free Courses
  • Certification Courses
  • 360+ Courses All in One Bundle
  • Login
Home Software Development Software Development Tutorials Shell Scripting Tutorial What is Bash Scripting?
Secondary Sidebar
Shell Scripting Tutorial
  • Shell Scripting Basics
    • What is Shell Scripting
    • Uses of Shell Scripting
    • Shell Scripting Commands
    • Shell Script Parameters
    • Variables in Shell Scripting
    • Shell Script Argument
    • Execute Shell Script
    • Shell Script Set Variable
    • Shell Script Operators
    • Function in Shell Scripting
    • Echo in Shell Scripting
    • if condition in shell script
    • If Statement in Shell Scripting
    • If Else in Shell Scripting
    • Switch Case in Shell Scripting
    • Shell Script Case
    • Loops in Shell Scripting
    • For Loop in Shell Scripting
    • While loop in Shell Scripting
    • Shell Script Set
    • Shell Script Sleep
    • Shell Script Usage
    • Eval in Shell Script
    • Shell Script Read File
    • What is Bash?
    • What is Bash Scripting
    • Bash Shell Script
    • Bash Script Arguments
    • Batch Scripting Commands
    • Bash Script Variables
    • Bash Set Variables
    • Bash Local Variables
    • Bash Variable in String
    • Bash Concatenate Strings
    • Bash Export Variable
    • Bash Alias
    • Bash Trim String
    • Bash Replace String
    • Bash Split String
    • Bash File
    • Shell Scripting Interview Questions
    • Bash String Length
    • Korn Shell Scripting

Related Courses

Shell Scripting Course

Kali Linux Training

Red Hat Training Course

What is Bash Scripting?

By Priya PedamkarPriya Pedamkar

What is Bash Scripting

Overview of Bash Scripting

Bash is the abbreviation of Bourne-again shell. UNIX shell runs the program in a command line interpreter so that the computer program has various dialects in the language. The language has many commands in the text which is a mix of different commands. The normal operations in any scripting language are file manipulation, program execution, and text printing. These scripts are mainly used for administrative tasks in the system. Bash has invocation features and the look can be customized in the scripting. Also, they lend a helping hand in installing complex programs. The files are saved as .bashrc.

How Does Bash Scripting Works?

Following are the steps to write and execute a bash script successfully:

Start Your Free Data Science Course

Hadoop, Data Science, Statistics & others

All in One Data Science Bundle(360+ Courses, 50+ projects)
Python TutorialMachine LearningAWSArtificial Intelligence
TableauR ProgrammingPowerBIDeep Learning
Price
View Courses
360+ Online Courses | 50+ projects | 1500+ Hours | Verifiable Certificates | Lifetime Access
4.7 (85,992 ratings)
  • Open any text editor (For ex vi or nano) from the command line. This can simply be done by typing vi or nano and pressing enter.
  • Put the following line at the beginning: #!/bin/bash. This is required to declare the interpreter. Here #! it is called shebang or hashbang and the remaining part is the path to the interpreter. You can get the path to your interpreter by executing the command: which bash
  • Now put all the commands you want to execute.
  • Save the file with a name and .sh extension.
  • Make the file executable with the following command: chmod +x <file_name>
  • Run the script with the following command: ./<file_name>

In case you need to pass some arguments you can pass them in the following way:

./<file_name>  arg1 arg2 arg2

Example

Let’s write and execute a bash script that would read marks obtained by various students in a subject (Say English) and display the average marks, maximum marks (and by which students) and minimum marks (and by which students). It will also list out the students who have failed in the subject.

Given a csv file whose each row consists of a student’s name and marks obtained. Let’s assume that the minimum marks (out of 100) to pass in the subject is 40.

Script:

#!/bin/bash
# Declaring all the variables
SUM=0
COUNT=0
MAX_MARKS=0
MIN_MARKS=100
MAX_MARKS_LIST=()
MIN_MARKS_LIST=()
FAIL_LIST=()
input="marks.csv"
# Set "," as the field limiter using $IFS
# and then read line by line using while and read
while IFS=',' read -r name marks
do
if [ $marks -gt $MAX_MARKS ]; then
MAX_MARKS=$marks
fi
if [ $marks -lt $MIN_MARKS ]; then
MIN_MARKS=$marks
fi
if [ $marks -lt 40  ]; then
FAIL_LIST+=($name )
fi
SUM=$(( SUM + marks))
COUNT=$(( COUNT + 1 ))
done < "$input"
input="marks.csv"
while IFS=',' read -r name marks
do
if [ $marks -eq $MAX_MARKS ]; then
MAX_MARKS_LIST+=($name)
fi
if [ $marks -eq $MIN_MARKS ]; then
MIN_MARKS_LIST+=($name)
fi
done < "$input"
echo "The highest marks obtained is:" $MAX_MARKS
echo
echo "The students who have obtained the highest marks are: " ${MAX_MARKS_LIST[@]}
echo
echo "The lowest marks obtained is:" $MIN_MARKS
echo
echo "The students who have obtained the lowest marks are:"  ${MIN_MARKS_LIST[@]}
echo
echo "The students who have failed are:" ${FAIL_LIST[@]}
echo
echo "The average marks obtained is:" $((SUM / COUNT))

We have put the above script inside a file called my_script.sh.

We have our input inside marks.csv file.

Let’s check its content:

what is bash scripting 1

Now let’s try to run the script:

what is bash scripting 2

We are getting this error because we haven’t made it executable yet. Let’s set proper permissions:

proper permissions

Let’s try to run it again:

marks obtained output

Bingo! We got the desired output.

Advantages of Bash Scripting

  • It can automate the frequently performed operations.
  • Can run a sequence of commands in a go.
  • Quite easy to use.
  • It is portable.

Conclusion

In this article, we have seen how we can execute a set of shell commands in one go. We can use Crontab in Linux/UNIX systems to schedule the run of a bash script at the desired time which would reduce manual efforts to large extents.

Recommended Articles

This has been a guide to What is Bash Scripting? Here we discuss the introduction, advantages and how does Bash Scripting work with a respective example. You may also have a look at the following articles to learn more –

  1. What is Shell Scripting?
  2. Batch Scripting Commands
  3. Function in Shell Scripting
  4. Bash Shell in Linux
Popular Course in this category
Shell Scripting Training (4 Courses, 1 Project)
  4 Online Courses |  1 Hands-on Project |  18+ Hours |  Verifiable Certificate of Completion
4.5
Price

View Course

Related Courses

Kali Linux Training (3 Courses, 3+ Projects)4.9
Red Hat Linux Training Program (4 Courses, 1+ Projects)4.8
1 Shares
Share
Tweet
Share
Primary Sidebar
Footer
About Us
  • Blog
  • Who is EDUCBA?
  • Sign Up
  • Live Classes
  • Corporate Training
  • Certificate from Top Institutions
  • Contact Us
  • Verifiable Certificate
  • Reviews
  • Terms and Conditions
  • Privacy Policy
  •  
Apps
  • iPhone & iPad
  • Android
Resources
  • Free Courses
  • Database Management
  • Machine Learning
  • All Tutorials
Certification Courses
  • All Courses
  • Data Science Course - All in One Bundle
  • Machine Learning Course
  • Hadoop Certification Training
  • Cloud Computing Training Course
  • R Programming Course
  • AWS Training Course
  • SAS Training Course

ISO 10004:2018 & ISO 9001:2015 Certified

© 2022 - EDUCBA. ALL RIGHTS RESERVED. THE CERTIFICATION NAMES ARE THE TRADEMARKS OF THEIR RESPECTIVE OWNERS.

EDUCBA
Free Data Science Course

SPSS, Data visualization with Python, Matplotlib Library, Seaborn Package

*Please provide your correct email id. Login details for this Free course will be emailed to you

By signing up, you agree to our Terms of Use and Privacy Policy.

EDUCBA Login

Forgot Password?

By signing up, you agree to our Terms of Use and Privacy Policy.

EDUCBA
Free Data Science Course

Hadoop, Data Science, Statistics & others

*Please provide your correct email id. Login details for this Free course will be emailed to you

By signing up, you agree to our Terms of Use and Privacy Policy.

EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you

By signing up, you agree to our Terms of Use and Privacy Policy.

Let’s Get Started

By signing up, you agree to our Terms of Use and Privacy Policy.

This website or its third-party tools use cookies, which are necessary to its functioning and required to achieve the purposes illustrated in the cookie policy. By closing this banner, scrolling this page, clicking a link or continuing to browse otherwise, you agree to our Privacy Policy

Loading . . .
Quiz
Question:

Answer:

Quiz Result
Total QuestionsCorrect AnswersWrong AnswersPercentage

Explore 1000+ varieties of Mock tests View more