Skip to main content

How to Write a Bash Script: A Simple Bash Scripting Tutorial

Discover the basics of bash scripting and learn how to write a bash script.
Nov 2022  · 5 min read

A shell is a kind of computer program that presents an interface to the operating system. The name shell was derived from the fact that it is the outermost layer around the operating system. 

To manage the interaction between users and the operating system, the shell prompts for an input and then interprets that input for the operating system before handling any outputs it receives. This input may be provided via a command line interface (CLI) or graphical user interface (GUI), depending on the operation and the role the computer takes on.

Check out our What is Shell? article to delve deeper into what a shell is and how it can help you. 

Essentially, a shell enables users and other programs to communicate directly with an operating system’s components.

A Primer on Shell Scripting 

Shell scripting is an important yet easy-to-learn (even if you are not from a programming background) part of communicating with a computer’s operating system. By scripting, we are referring to a sequence of shell and operating system commands that are written and then stored in a file: typical operations performed by shell scripts include tasks such as file manipulation, program execution, and printing text. 

Learn more about Data Processing in Shell with our course. 

Learning to perform this simple procedure can save developers a significant amount of development time as certain commands will not have to be manually written repeatedly – it is even possible to schedule scripts to be executed automatically. 

There are several different shells available to Unix and Linux users. For the remainder of this tutorial, we will focus specifically on the Bourne Again SHell (bash), which is an enhanced version of the Bourne shell and is one of the most common command line interfaces for Unix-based operating systems (including Linux). 

What is a Bash Script? 

Bash is a Unix command line interface responsible for interacting with a computer's operating system. Similarly to how movie scripts inform actors of what actions to take, a bash script tells the bash shell what to do. Thus, a bash script is a useful way to group commands to create a program

Any command you could execute directly from the command line can be put into a bash script, and you could expect it to do the same actions as it would from the command line. Alternatively, any command you enter into your bash script could be run directly from the command line, and the result should be the same. In other words, if you know how to interact with your computer from the command line, you already know a fair bit about bash scripting. 

One of the main differences is a bash script is a plain text file that is stored with a .sh extension by convention (they can still run without the .sh extension). 

Another distinction of bash scripts is the “shebang” that occurs on the first line of all scripts. Essentially, the shebang is an absolute path to the bash interpreter made up of a combination of bash “#” and bang “!” followed by the bash shell path. It is used to tell the Linux operating system which interpreter to use to parse the file. 

How to Write a Bash Script

We are going to build a simple “Hello World” bash script example to get you started with bash scripting. If you wish to delve deeper into bash scripting, check out Introduction to Bash Scripting course. 

Step 1: Create a new plain text file 

The first step is to create a new plain text file. You can do this by running the following command from your command prompt: 


Note: Executing this command may bring a prompt asking you if you would like to create a new file; select “Yes.” 

Step 2: Specifying the interpreter

On the first line of our script, we must specify which interpreter we would like to use to parse our script. In this scenario, it is Bash. Thus, we must put the shebang in the first line of our script. 


Step 3: Implement commands

The purpose of our bash script is to print “Hello World!” To perform this task, move to a new line and use the echo command followed by the string we would like to print.

echo "Hello World!" 

Once complete, save the file and open up your Git Bash.  

Note: You can download Git Bash from Git downloads. Simply select the version of Git that corresponds with your operating system. 

Next, run the following command: 


This should output “Hello World!” as seen in Figure 1. 

A call to the bash script returns “Hello World!” in the terminal.

Figure 1: The output of our Bash script


Bash scripts are a useful way to elegantly group commands together. You could leverage the power of a bash script for a number of use cases, such as executing a shell command, running multiple commands together, customizing administrative tasks, performing task automation, and more. 

By following the steps provided in the tutorial above, you now know how to create a simple script to print “Hello World!” using Bash. 

Introduction to Shell

4 hours
The Unix command line helps users combine existing programs in new ways, automate repetitive tasks, and run programs on clusters and clouds.
See DetailsRight Arrow
Start Course

Data Processing in Shell

4 hours
Learn powerful command-line skills to download, process, and transform data, including machine learning pipeline.

Introduction to Bash Scripting

4 hours
Bash scripting allows you to build analytics pipelines in the cloud and work with data stored across multiple files.
See all coursesRight Arrow

5 Ways to Use Data Science in Marketing

Discover five ways you can use data science in marketing. Get ahead of the game, improve your data skills, and work on a data science marketing project.
Natassha Selvaraj's photo

Natassha Selvaraj

What is Business Analytics? Everything You Need to Know

We explain what business analytics is, why it's important, the different aspects of business analytics, and the tools most commonly used.
Joleen Bothma's photo

Joleen Bothma

How to Analyze Data For Your Business in 5 Steps

Discover the different steps to analyze data and extract value from it, as well as the methods and techniques involved in the process.
Javier Canales Luna 's photo

Javier Canales Luna

14 min

What is Data Maturity and Why Does it Matter?

Discover what data maturity is and why it matters to businesses of all sizes. Plus, find out how to determine your company's data maturity.
Elena Kosourova 's photo

Elena Kosourova

10 min


The Deep Learning Revolution in Space Science

Justin Fletcher joins the show to talk about how the US Space Force is using deep learning with telescope data to monitor satellites, potentially lethal space debris, and identify and prevent catastrophic collisions. 

Richie Cotton's photo

Richie Cotton

53 min

Regular Expressions Cheat Sheet

Regular expressions (regex or regexp) are a pattern of characters that describe an amount of text. Regular expressions are one of the most widely used tools in natural language processing and allow you to supercharge common text data manipulation tasks. Use this cheat sheet as a handy reminder when working with regular expressions.
DataCamp Team's photo

DataCamp Team

See MoreSee More