How to Create Your First Linux Bash Script
Bash scripts are often used for backups, to set variables, update your operating system, and much more. With a bit of creativity, there's no limit to what you can do with Bash.
Aug 16th, 2024 5:00pm by
Featured image from The New Stack.
Creating a ‘Hello, World!’ Bash Script
As you probably guessed, you’re going to need a text editor to create your Bash scripts. Because these are command-line only, I tend to prefer a basic text editor. My text editor of choice is nano. Create your script file with the command:nano hello.shYou don’t have to add the
.sh extension, I do so that I can more easily discern a script file from a basic text file. Do know that .sh is the official extension for shell scripts.
The first line you’ll add is this:
#!/bin/bashWhat is that? The above line is often referred to as “shebang”) and informs the shell interpreter which application to use. (In this case, it’ll use
bash.)
We can finish our script with a single line that is:
echo "Hello, New Stack!"Our entire script looks like this:
#!/bin/bash echo "Hello, New Stack!"Save and close the file. There are two ways to run the script. The first is by using the
sh command (the dash command interpreter) like this:
sh hello.shThe output will be:
Hello, New Stack!
chmod u+x hello.shNow, to run the command you can run:
./hello.shThe reason this is the better alternative is that you could move that file to any directory in your user $PATH and then run the command from any directory in the file system hierarchy. You could move it to
/usr/local/bin with the command:
sudo mv hello.sh /usr/local/binOnce you’ve done that, change the ownership with a command like:
sudo chown $USER.$USER /usr/local/bin/hello.shAt this point, you could run the command by simply typing:
hello.shLet’s create a backup script with Bash.
Creating a Backup Script with Bash
Let’s say you want to back up/var/www/html on a nightly basis and you want to back it up to an external drive mounted at /media/USER/backup (where USER is your username). What we’ll do is create a simple compressed tar file of the /var/www/html directory to the backup drive and attach the date to the file name.
Create a new script file with the command:
nano backup.shThe first line in the script will be:
#!/bin/bashNext, we’re going to set a variable for the date with:
DATE=`date +%b-%d-%y`What we’ve done above is set the variable
DATE with the command date +%b-%d-%y, so the variable will always be the time and date when the script is run. Take note that the command is enclosed in backticks (and not single quotes), which is required for it to function.
Next, we’ll set a variable for the destination of our backup, which is /media/USER/backup (where USER is your username). That variable is set with the line:
DESTINATION=/media/USER/backup/HTML-BACKUP-$DATE.tar.gzThe important thing to take notice of in the above line is
$DATE. What that does is inform Bash that what follows the $ is a variable. In our case, it will add the current time/date after BACKUP- and before .tar.
The final variable we’ll set is the source folder (which is the folder to be backed up). In our case, the source folder is defined as:
SOURCE=/var/www/htmlThe last piece of the puzzle is the actual command that runs for the backup, which is:
tar -cpzf $DESTINATIOn $SOURCEThe entire script looks like this:
#!/bin/bash DATE=`date +%b-%d-%y` DESTINATION=/media/USER/backup/HTML-BACKUP-$DATE.tar.gz SOURCE=/var/www/html tar -cpzf $DESTINATION $SOURCESave and close the file. We’ll now give the file executable permissions with:
chmod u+x backup.shRun the script with:
sudo ./backup.shWe use sudo because
/var/www/html/ requires elevated permissions.
You should now see a compressed archive file of your backup in the destination directory.
If you want to automate the script, I would suggest moving the file to /usr/local/bin (as I explained earlier) and then create a cron job with:
sudo crontab -eAt the bottom of the file, add something like this (to run the script at midnight every day): We have to use
> /dev/null 2>&1 to suppress the output of the command, otherwise, the cron job will fail.
And that’s how you take your first steps with Bash scripts. We’ll revisit this topic another time to create more complex Bash scripts (which could include loops!).
YOUTUBE.COM/THENEWSTACK
Tech moves fast, don't miss an episode. Subscribe to our YouTube
channel to stream all our podcasts, interviews, demos, and more.