Sure, bash scripts can get very complex – but at first they can be as simple as you want them to be. degreez/Getty Images
I’ve been using Linux for a very long time, during which time I’ve done everything you can imagine with the open-source operating system. From my early days, one thing that I needed to learn was to create bash scripts. 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 detects that I am offline, it will reconnect me.
Thankfully, I no longer have to resort to such tricks. In fact, Linux has become so user-friendly that I rarely have to bother with bash scripts. Still, it’s a great feature to have handy.
Too: How to choose the right Linux desktop distribution for you
What is bash script?
Think of a Bash script as a small application you create that contains Linux commands. You can write Bash scripts to do just about anything, such as creating backups, setting variables, opening applications, navigating to specific directories, creating files, and more. Really, with just a little creativity, the sky’s the limit when it comes to Bash scripts.
But remember, bash scripts are just that…scripts. They are not GUI applications, nor 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.
This does not mean that bash scripts are only for advanced users. Sure, bash scripts can be very complex but in the beginning they can be as simple as you want.
I’m going to create and demonstrate two different bash scripts. First, we tried the tried-and-true “Hello, World!” do. And then we’ll create a bash script that will back up a directory.
are you ready let’s go.
How to make Hello World! bash script
what you’ll need: The only thing you will need for this is a running instance of Linux. Since every distribution supports bash scripts, it doesn’t matter which one you use. Having prepared it, let’s create.
The first thing you should do is open the Terminal window, which can be found in your Desktop menu.
Our first script file will be called hello_world.sh. Create file with command:
The first line in each bash script is:
The second line of the script Hello, World! parts. First, we’ll add a comment indicating what the next command does, which might look like this:
# My Hello, World! script
Finally, for bash scripts use the command echo Order as follows:
Put it all together and it would look 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 this, issue the command:
The output of the command “Hello, World!” Should be
Too: The most important reason why you should use Linux at home
Backup with 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 are also going to set variables that will be used to add the date to the backup file name, set the destination as well as set the source. we will also use Tar command for actual backup.
The first thing to do is to create a new file with the command:
Too: best linux distros for beginners
1. Create new file
The first thing to do is to create a new file with the command:
2. Create Script
The complete 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 the BACKUPDATE script will be the date it was run). For example, if you run the backup script on July 24, 2023, the file will be named DOCUMENTS-Jul-24-23.tar.gz.
3. Give executable permissions to the script
To make it so 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 little creativity, you can do a lot with these handy little command-line applications.
Too: My idea for a great new beginner-friendly Linux distribution










