How to create a Linux bash script - and what you can do with it

4 days ago 2
abstract concept
Tolga_TEZCAN/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 was how to 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 created a bash script that would monitor my connectivity; if the script discovered I was offline, it would reconnect me.

Also: I've used virtually every Linux distro, but this one has a fresh perspective

Thankfully, I no longer have to perform such tricks. In fact, Linux has become so user-friendly that 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.

Also: 5 factors steadily fueling Linux's desktop rise

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. Neither of these scripts is nearly as challenging as you might think.

Also: 5 surprisingly productive things you can do with the Linux terminal

Are you ready? Let's go.

How to create the Hello, World! bash script

What you'll need: The only thing you'll need 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.

Show more

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

Show more

The first line in every bash script is:

Show more

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:

Show more

To run your first bash script, issue the command:

Show more

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

Also: The first 5 Linux commands every new user should learn

How to create 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 allow the script to run, give it executable permissions with the command:

4. Run the script

To run the new backup bash script, the command is:

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.

What is the purpose of a bash script?

A bash script (also known as a shell script) is a file that contains commands written in the Bourne-Again SHell (Bash). It allows you to automate tasks, perform repetitive operations, and simplify system administration.

How do I create a new bash script?

  1. Open your favorite text editor (e.g., nano, vim).
  2. Save the file with a .sh extension (e.g., myscript.sh).
  3. Edit and save the script.

How do I run a bash script?

  1. Open a terminal window.
  2. Navigate to the directory where your script is located using the cd command (e.g., cd /path/to/script/).
  3. Give the script executable permissions (such as with the command chmod u+x myscript.sh).
  4. Type ./myscript.sh and press Enter.

What are some common bash shell variables?

  • $PATH: The path to the system's executable files.
  • $HOME: Your home directory.
  • $USER: Your username.
  • $RANDOM: A random number generator.

How do I use variables in a bash script?

To use a variable in a bash script, you must assign a value to a variable using the = operator (e.g., var="my_value").
Use the variable within your script by typing its name preceded with the $ character (as in echo "My name is $name.)

What are some common bash conditional statements?

  • The if statement evaluates a condition and then executes code if true.
  • The elif statement checks another condition if the first one fails and then executes additional code if true.
  • The else statement executes alternative code when no conditions match.

Get the morning's top stories in your inbox each day with our Tech Today newsletter.

Show more

Read Entire Article