After using Linux for a while you will eventually find yourself needing to create a bash script. And just what is a bash script? A bash script is a script that is run through the bash shell. Usually a bash script is a user-generated script that serves a specific purpose and combines numerous commands into one convenient script.
I am a big fan of bash scripting. I use them frequently to create backup scripts, scripts that are used via cron, and much more. But before I could write even a simple bash script, I had to understand the basics. And that's what you will learn here - the very basics of bash scripting. This will be a foundation you can build upon so that your bash scripts can get more and more complex.
A bash script consists of just a few pieces. First and foremost you have to actually create the file. This file will contain all of your scripting and will have to be made executable by the user. Once the file is complete and saved you will make this executable with the command:
chmod u+x FILENAME
Where FILENAME is the actual name of your file.
Now within the file you will have to at least have two minimal pieces:
The shell declaration is a statement that declares what shell you are to use. For nearly all of your Linux needs you will use the bash shell. To declare the bash shell being used your declaration will be:
With that declaration all commands will be run through the bash shell.
The script is the contents of the shell script you will write. The script will most often consist of commands.
Ah hello world! Who hasn't or used this as an example. Let's take a look at what an Hello World script wold look like. We'll add a few variations to highlight some of the subtle differences.
The basic Hello World! script would look like:
echo "Hello World!"
Once you save it (we'll call it "hello") and make it executable you can run it by issuing the command:
and you will see the output:
Now let's use variable declaration in this script. Using variables will make your scripting much more versatile.
echo $STRING1 $STRING2
Now let's modify this to use a global variable. One useful global variable is USER. At the bash prompt enter echo $USER and bash will return the username that is currently logged in. So change your hello world script to look like:
echo $STRING1 $USER
When you run this script you will see:
Where USER is the actual username logged in. You can test this by su'ing to a different user (such as root) and running the script. If you are root (make sure you su to root with the command su - or you won't have root's prompt, only root's privileges) you will see:
And there you have the very fundamentals of bash scripting. Hopefully you can see how to build on the Hello World! example.
Advertising revenue is falling fast across the Internet, and independently-run sites like Ghacks are hit hardest by it. The advertising model in its current form is coming to an end, and we have to find other ways to continue operating this site.
We are committed to keeping our content free and independent, which means no paywalls, no sponsored posts, no annoying ad formats (video ads) or subscription fees.
If you like our content, and would like to help, please consider making a contribution:
Ghacks is a technology news blog that was founded in 2005 by Martin Brinkmann. It has since then become one of the most popular tech news sites on the Internet with five authors and regular contributions from freelance writers.