X
Tech
Why you can trust ZDNET : ZDNET independently tests and researches products to bring you our best recommendations and advice. When you buy through our links, we may earn a commission. Our process

'ZDNET Recommends': What exactly does it mean?

ZDNET's recommendations are based on many hours of testing, research, and comparison shopping. We gather data from the best available sources, including vendor and retailer listings as well as other relevant and independent reviews sites. And we pore over customer reviews to find out what matters to real people who already own and use the products and services we’re assessing.

When you click through from our site to a retailer and buy a product or service, we may earn affiliate commissions. This helps support our work, but does not affect what we cover or how, and it does not affect the price you pay. Neither ZDNET nor the author are compensated for these independent reviews. Indeed, we follow strict guidelines that ensure our editorial content is never influenced by advertisers.

ZDNET's editorial team writes on behalf of you, our reader. Our goal is to deliver the most accurate information and the most knowledgeable advice possible in order to help you make smarter buying decisions on tech gear and a wide array of products and services. Our editors thoroughly review and fact-check every article to ensure that our content meets the highest standards. If we have made an error or published misleading information, we will correct or clarify the article. If you see inaccuracies in our content, please report the mistake via this form.

Close

What is a Linux bash script and how do you create one?

Bash scripts have been around since the humble beginnings of Linux. But what are they and how do you create your first one? Find the answers to those questions here.
Written by Jack Wallen, Contributing Writer
bashgettyimages-1163541985

Sure, bash scripts can get very complicated -- but at first they can be as simple as you like.

Deagreez/Getty Images

I've been using Linux for a very long time, during which I've done just about everything you can imagine with the open-source operating system. From my early days, one thing I needed to learn how to do was create a bash script. When I first started using Linux, I had a 33.6k modem that refused to stay online. To get around that problem, I had to create a bash script that would monitor my connectivity; if the script discovered I was offline, it would reconnect me.

Also: The best Linux laptops right now

Thankfully, I no longer have to pull off such tricks. In fact, Linux has become so user-friendly, I rarely have to bother with bash scripts. Even so, it's a great feature to have handy.

What is a bash script?

Think of a bash script as a tiny application you create that consists of Linux commands. You can write bash scripts to do just about anything, such as create backups, set variables, open applications, navigate to specific directories, create files, and so much more. In fact, with just a little creativity, the sky's the limit with bash scripts.

But remember, bash scripts are just that…scripts. They aren't GUI applications, nor is there a GUI application that will walk you through the process of creating a script. In other words, bash scripts are a bit more advanced than using a word processor, web browser, or email client. 

That doesn't mean bash scripts are for advanced users only. Sure, bash scripts can get very complicated but at first they can be as simple as you like.

Also: How to choose the right Linux desktop distribution for you

I'm going to demonstrate creating two different bash scripts. First, we'll do the tried-and-true "Hello, World!" and then we'll create a bash script that will back up a directory.

Are you ready? Let's go.

How to create the Hello, World! bash script

What you'll need: The only thing you'll need for this is a running instance of Linux. Since every distribution supports bash scripts, it doesn't matter which one you use. With that at the ready, let's create.

1. Open the terminal window

The first thing you must do is open the terminal window, which can be found within your desktop menu.

2. Create the new file

Our first script file will be called hello_world.sh. Create the file with the command:

nano hello_world.sh

3. Create the bash script

The first line in every bash script is:

#!/bin/bash

The second line of the script is the Hello, World! portion. First, we'll add a comment that indicates what the next command does, which might look like this:

# My Hello, World! script

Finally, the command for the bash script uses the echo command like so:

echo "Hello, World!"

Put it all together and it looks like this:

#!/bin/bash

# My Hello, World! script
echo "Hello, World!"

Save and close the file with the Ctrl+X keyboard shortcut.

4. Give the script executable permissions

Before the script can be run, it must have executable permissions. To do that, issue the command:

chmod +x hello_world.sh

5. Run the command

To run your first bash script, issue the command:

./hello_world.sh

The output of the command should be "Hello, World!"

Also: The most important reason you should be using Linux at home

Creating a backup with a bash script

Let's say you want to back up your Documents directory to an external drive located at /media/USER/data (Where USER is your Linux username). With this backup, we're also going to set variables that will be used to append the date to the backup file name, set the destination, as well as set the source. We'll also use the tar command for the actual backup.

The first thing to do is create the new file with the command:

nano backup.sh

Also: The best Linux distros for beginners

1. Create the new file

The first thing to do is create the new file with the command:

nano backup.sh

2. Create the script

The entire script will look something like this (where USER is your Linux username):

#!/bin/bash

# set variables for data, destination, and source

BACKUPDATE=`date +%b-%d-%y` 

DESTINATION=/media/USER/data/DOCUMENTS-$BACKUPDATE.tar.gz 

SOURCEFOLDER=/home/USER/Documents 

# the backup command

tar -cpzf $DESTINATION $SOURCEFOLDER #create the backup

The Destination variable not only sets the location for the backup, but it also names the backup DOCUMENTS-BACKUPDATE.tar.gz (where BACKUPDATE will be the date the script is run).  For example, if you run the backup script on July 24, 2023, the file name would be DOCUMENTS-Jul-24-23.tar.gz. 

3. Give the script executable permissions

To make it such that the script can be run, give it executable permissions with the command:

chmod u+x backup.sh

4. Run the script

To run the new backup bash script, the command would be:

./backup.sh

And that's all it takes to create your first bash script. As I said, with a bit of creativity, you can do so much with these handy little command-line applications. 

Also: My idea for a great new beginner-friendly Linux distribution

Editorial standards