In this lecture, we'll start talking about the variables in Bash. Variables are a vital part of any 1
programming language, including Bash. 2
A variable is nothing more than a name for a memory location where a value, which can be manipulated 3
is stored. To create a new variable write to the name of the desired variable, followed by the assignment 4
operator or the equal sign and the value of the variable. 5
You can create the variable in a script file or directly in the shell that's running in the terminal. Just 6
to make it easier 7
for this example, I'm creating a variable in the terminal. 8
So os=Linux 9
The name of the variable is os and its value 10
the string Linux. 11
Note that it is not allowed to use the space before or after the equal sign. 12
Like this, it's not allowed. 13
I've got an error! Even though in other programming languages, this is recommended for readability, 14
it's not permitted in bash. 15
If the value of a variable is a string that contains spaces, then enclose everything between double 16
quotes; for example distro 17
= and between double quotes, a string that contains white spaces, like say MX Linux 18
Compared to other programming languages like C, Java or Go lang, where each variable has a type, 19
Bash is weakly typed programming language that does not require you to define any data type at 20
time of a variable declaration. 21
So when any numeric value is assigned to a variable, then it will work as an integer. 22
And when any text value is assigned to a variable, then it is a string. 23
In Bash, there are no floating point numbers only integers. 24
This is an example of a variable which stores a number age=30 25
OK, let's talk about variable naming conventions! The variable names should be descriptive and 26
remind you of the value they hold. 27
A variable name cannot start with a number, nor can it contain spaces or other special characters like 28
dots, colons, semicolons, exclamation or question marks and so on. 29
All the following variable names are illegal 4you=True 30
This is an illegal variable name. 31
It's not allowed to start with a number. 32
Or a.b=100 , it's not allowed to have special characters like dots or 33
Anne@Marry 34
It can, however, start with an underscore, and apart from that, you can use any mix of upper and 35
lower case alphanumeric characters. 36
These are all legit variable names. 37
you4=False 38
Or server_name=”Apache 2.5” 39
Or A=34 40
Giving a variable a value is known as assigning a value to a variable and getting the value stored in a 41
variable is known as referencing the value of a variable. 42
To access the value of a variable precede the name of the variable with a dollar sign.Let's see the values 43
of the variables we've just created and for that I'll use echo so: echo $ and the name of the variable 44
age or echo $os 45
or echo $you4 46
I am referencing the values of the variable. 47
You can also reference the value of a variable inside the string by enclosing everything in double quotes 48
like this: echo "I'm learning 49
$os and I'm $age old." 50
In this example, the name of the variable is simply replaced by its value. 51
Note that we'll get another result if we use single quotes instead of double quotes like this:$ 52
os some other string and $age 53
The name of the variable is not replaced by its value when we use single quotes instead of double quotes. 54
If you want to cancel the special meaning of the dollar sign or any other special character, just use 55
a backslash before the special character. 56
So echo "The value of \$os is $os" 57
In this example, the backslash symbol is canceling the special meaning of the dollar sign and its printing 58
$os literally instead of the available value. 59
So it's not referencing the value of the variable. 60
You can also create a variable that takes its value from an existing variable or a number of variables. 61
Let's say distro=”Ubuntu” , and my_distro="$os $distro" 62
And echo $my_distro of course, using a dollar sign. 63
And it printed out, Linux, a white space and Ubuntu 64
To get a list of all shell variables and functions run the set command. 65
There are so many, so it's better to pipe the output to less or to grep if you want to search for a 66
specific variable like say set | grep distro 67
I was searching for the distro variable and I see the variable and its value. 68
To undefine or remove a variable use the unset command: echo $distro; unset distro 69
without the dollar sign. 70
I'm not referencing the value of the variable and echo $distro 71
Distro is not defined anymore. 72
It's empty. 73
Another rule of thumb about naming variables is that environment or social variables that are 74
introduced by the operating system, 75
shell startup scripts or by the shell itself are usually all in capitals. Let's see such variables! 76
echo $PATH 77
USER 78
The user currently logged in; HOME, the home directory of the current user; HISTFILEe or HISTSIZE 79
These are all environment variables and are in capitals. To prevent your own variables from conflicting 80
with these variables 81
it's a good practice to use lower case variable names. 82
Sometimes you want to make sure that the content of a variable does not change, it's read-only. To declare such 83
a constant use the declare built in command with the -R option, for example: declare -R logdir 84
= and the value var/log logdir is a constant. 85
We can use the variable in our script, as usual; for example, ls $logdir If you try 86
to change the value held in the variable, you'll get an error. 87
For example: logdir=abc It is a read only variable and you cannot it or delete the only variable. 88
At the end of this lecture, let's create a script that uses variables. The script will display the 89
number of lines, words and characters in a text file, and the first five and the last seven lines of 90
the file. The name of the file, including its absolute path, will be stored in a variable. 91
So I'm creating a new script, let's say, file_stat.sh 92
The first line will be the shebang. 93
I'm defining a new variable called filename= and the absolute path to a file, let's say /etc/passwd 94
echo. 95
Number of lines and I'm running a shall commend wc-l and the value of the variable so 96
$filename 97
I'm printing out a series of hash signs for readability. 98
And now the first five lines echo "First 5 lines:" and the command head -n 5 99
$filename 100
And the last lines echo "Last 7 lines:" and the commmand tail -n 7 $filename 101
That's the script! 102
I'm saving and exiting the file. I'm setting the execution permission for the file, and I'm running 103
it. 104
It's working as we have expected.