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.
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.
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 moreOur first script file will be called hello_world.sh. Create the file with the command:
Show morenano hello_world.sh
The first line in every bash script is:
Show more#!/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 theechocommand like so:
echo "Hello, World!"
Put it all together and it looks like this:
#!/bin/bash#My Hello, World! scriptecho "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 morechmod +x hello_world.sh
To run your first bash script, issue the command:
Show more./hello_world.sh
The output of the command should be "Hello, World!"
Also: The first 5 Linux commands every new user should learn
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 thetarcommand 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
The first thing to do is create the new file with the command:
nano backup.sh
The entire script will look something like this (where USER is your Linux username):
#!/bin/bash#set variables for data, destination, and sourceBACKUPDATE=`date +%b-%d-%y` DESTINATION=/media/USER/data/DOCUMENTS-$BACKUPDATE.tar.gz SOURCEFOLDER=/home/USER/Documents#the backup commandtar -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.
To allow the script to run, give it executable permissions with the command:
chmod u+x backup.sh
To run the new backup bash script, the command is:
./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.
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.
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 inecho "My name is$name.)
Get the morning's top stories in your inbox each day with ourTech Today newsletter.
Show more