In this video, I would like to tell you
a bit about variables and positional parameters.
So variables are used to make shell scripts dynamic.
They're pretty important.
And site-specific information is stored in a variable.
And that can be picked up dynamically
while running the script.
So the idea is that you write a very generic script.
And this generic script doesn't have any specifics.
And the specifics are picked up from the variable.
And they make sure that the script
is adopted to specific requirements on your site.
If an argument is passed to the script while it is started,
this argument, which is known as a positional parameter,
is further treated as a variable.
Let me show you how it works.
So let's call it script2.
And script2 is going to do what?
Bin bash.
So let me use color is red.
And then echo the color is dollar color.
That's a very basic script.
And we probably need to enhance it.
But before we do so, let's run it.
Now we can see the color is red.
Now, there is one thing that you shouldn't
like about this script.
And that is the use of the variable.
Why do you define a variable if this variable is statically
defined in the script?
The purpose of variables is that the script is picking up
variables in a dynamic way.
And you can do that in different ways.
And one way would be to use color equals dollar 1.
What does that mean?
That means that a variable color is going to be set.
But this variable color is going to be set
based on the argument that is provided
while running the script.
So then, if you run script2 without an argument,
you get something ugly.
And that is the color is.
And you don't see anything.
But if you make that script2 red, then it will print.
The color is red.
And the color is blue, and so on.
That's a little bit better.
Now, something that is also commonly done
is where variables are sourced from somewhere else.
So let's create an additional file with the name vars.
And in this additional file vars, I'm setting color is red.
So the idea is that on your system,
you have a directory that contains these variable
configuration files.
And when you run your script, it picks up the variables
from these configuration files.
So I'm going to edit my script again.
And this time, I'm not setting the variable right here.
But I'm going to source this vars file.
Sourcing means including.
And you can do that two ways.
Either you type source, or you type
dot, dot followed by the name of the file
that you want to source.
Notice that that should be dot space,
because dot is really a command.
It's a shortcut for the source command.
So if I run it right now, I can see script2.
It has the argument blue, but the argument is ignored.
But it is printing the color is red.
And that is because it's picked up from this vars file.
And if I want to change that, let me change it.
We can see that it is picked up from the vars file again,
even if we don't provide the argument while running
the script.
Now, this is a nice way of separating
the static and portable code in your script
and having your script pick up specific variables
on a specific site.
And that's also how many Linux utilities are working.
You provide some generic information.
This generic information is written to a variable.
And you have a script that picks up the variable
to make your script do whatever it
needs to do in your environment.