Copyright

Bash Scripts: Variables & Parameters

Instructor: Martin Gibbs

Martin has 16 years experience in Human Resources Information Systems and has a PhD in Information Technology Management. He is an adjunct professor of computer science and computer programming.

A script can be useful on its own, especially for repetitive tasks. Sometimes we need to tweak a feature of the script, whether it's a date or a value we know will change. In this lesson, we'll explain how to build bash scripts that are dynamic.

Bash Scripting

... And we're off with a bang. Literally. Remember that all bash scripts start with the shebang, the #!/bin/bash keyword. Also recall that we can use these scripts to help us automate routine processes, such as file move/copy, or file backup.

But what if you want to create a file/folder backup script that creates a date-stamped file? This is useful if you are backing up some information but want to keep the versions. A simple Bash script will not do. You need to create a variable for your script.

Variables

Before we create the script to complete file backup, let's look at how a variable is created using Bash. The following sample declares a variable to store a folder location. Notice that we included the shebang at the beginning of the code, and added some screen text. The variable is declared in all caps.

Bash script basic variable

Now let's go a little deeper and create the backup script. In our example, we'll create the variable for the backup (DSTAMP). The code after the name of the file (backup-file) is special code that accurately formats the current date. The extension of the backup file is .tgz, which is like a windows ZIP file.

bash date variable

Parameters

Sometimes you need to let someone else define the value of something. The use of a parameter can be very powerful. Think of a parameter as a question to the user: Which file do you want me to move? Or, Tell me the name of the folder and I will rename it. By adding a parameter to our scripts, we allow for a lot of flexibility in the script. The intent is not to have to go back and re-edit our scripts every time!

Reading a parameter is fairly straightforward: You use the read keyword, followed by the parameter name. You could think of the parameter as another type of variable, and you'd be correct! Unlike the date variable, however, this is one provided by you, or the end user.

Below is a sample script that asks the user for the name of the file they are looking for.

Bash basic parameter

When we run the bash program, we're prompted to enter the file name:

Bash enter parameters screen

We can then enter the name of the file:

Bash enter the parameter

To unlock this lesson you must be a Study.com Member.
Create your account

Register to view this lesson

Are you a student or a teacher?

Unlock Your Education

See for yourself why 30 million people use Study.com

Become a Study.com member and start learning now.
Become a Member  Back
What teachers are saying about Study.com
Try it risk-free for 30 days

Earning College Credit

Did you know… We have over 200 college courses that prepare you to earn credit by exam that is accepted by over 1,500 colleges and universities. You can test out of the first two years of college and save thousands off your degree. Anyone can earn credit-by-exam regardless of age or education level.

To learn more, visit our Earning Credit Page

Transferring credit to the school of your choice

Not sure what college you want to attend yet? Study.com has thousands of articles about every imaginable degree, area of study and career path that can help you find the school that's right for you.

Create an account to start this course today
Try it risk-free for 30 days!
Create An Account
Support