Technology

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


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.

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.

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

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.

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.

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

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

The first line in every bash script is:

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:

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.

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

To run your first bash script, issue the command:

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:

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:

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:

4. Run the script

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

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





Source link