What is a variable?

Variables are a standard building block used in many (if not all) programming and scripting languages. Variables allow us to store information, so we can reference and use it later, often multiple times. We can, for example, use the textvariable variable to store the sentence This text is contained in the variable. In this case, the variable name of textvariable is referred to as the key, and the content of the variable (the text) is referred to as the value, in the key-value pair that makes up the variable.

In our program, we always reference the textvariable variable when we need the text. This might be a bit abstract now, but we're confident that after seeing the examples in the rest of the chapter, the usefulness of variables will become clear.

We've actually already seen Bash variables in use. Remember, in Chapter 4, The Linux Filesystem, we looked at both the BASH_VERSION and PATH variables. Let's see how we can use variables in shell scripting. We'll take our hello-world-improved.sh script, and instead of using the Hello world text directly, we'll first put it in a variable and reference it:

reader@ubuntu:~/scripts/chapter_08$ cp ../chapter_07/hello-world-improved.sh hello-world-variable.sh
reader@ubuntu:~/scripts/chapter_08$ ls -l
total 4
-rwxrwxr-x 1 reader reader 277 Sep 1 10:35 hello-world-variable.sh
reader@ubuntu:~/scripts/chapter_08$ vim hello-world-variable.sh

First, we copy the hello-world-improved.sh script from the chapter_07 directory into the newly created chapter_08 directory, with the name hello-world-variable.sh. Then, we use vim to edit it. Give it the following contents:

#!/bin/bash

#####################################
# Author: Sebastiaan Tammer
# Version: v1.0.0
# Date: 2018-09-01
# Description: Our first script using variables!
# Usage: ./hello-world-variable.sh
#####################################

hello_text="Hello World!"

# Print the text to the terminal.
echo ${hello_text}

reader@ubuntu:~/scripts/chapter_08$ ./hello-world-variable.sh
Hello World!
reader@ubuntu:~/scripts/chapter_08$

Congratulations, you've just used your first variable in a script! As you can see, you can use the content of a variable by wrapping its name inside the ${...} syntax. Technically, just putting $ in front of the name is enough (for example, echo $hello_text). However, in that situation, it is hard to differentiate where the variable name ends and the rest of the program begins—if you use the variable in the middle of a sentence, for example (or, even better, in the middle of a word!). If you use ${..}, it's clear that the variable name ends at }.

At runtime, the variable we defined will be replaced with the actual content instead of the variable name: this process is called variable interpolation and is used in all scripting/programming languages. We'll never see or directly use the value of a variable within a script, since in most cases the value is dependent on runtime configurations.

You will also see that we edited the information in the header. While it's easy to forget it, if a header does not contain correct information, you reduce readability. Always make sure you have an up-to-date header!

If we further dissect the script, you can see the hello_text variable is the first functional line after the header. We call this assigning a value to a variable. In some programming/scripting languages, you first have to declare a variable before you can assign it (most of the time, these languages have shorthand in which you can declare and assign as a single action).

The need for declaration comes from the fact that some languages are statically typed (the variable type—for example, string or integer—should be declared before a value is assigned, and the compiler will check that you're doing it correctly—for example, not assigning a string to an integer typed variable), while other languages are dynamically typed. For dynamically typed languages, the language just assumes the type of the variable from what is assigned to it. If it is assigned a number, it will be an integer; if it is assigned text, it will be a string, and so on.

Basically, variables can be assigned a value, declared, or initialized. While, technically, these are different things, you will often see the terms being used interchangeably. Do not get too hung up on this; the most important thing to remember is you're creating the variable and its content!

Bash does not really follow either approach. Bash's simple variables (excluding arrays, which we will explain later) are always considered strings, unless the operation explicitly specifies that we should be doing arithmetic. Look at the following script and result (we omitted the header for brevity):

reader@ubuntu:~/scripts/chapter_08$ vim hello-int.sh 
reader@ubuntu:~/scripts/chapter_08$ cat hello-int.sh
#/bin/bash

# Assign a number to the variable.
hello_int=1

echo ${hello_int} + 1
reader@ubuntu:~/scripts/chapter_08$ bash hello-int.sh
1 + 1

You might have expected that we would get the number 2 printed. However, as stated, Bash considers everything a string; it just prints the value of the variable, followed by the space, the plus sign, another space, and the number 1. If we want to have the actual arithmetic performed, we need a specialized syntax so that Bash knows that it is dealing with numbers:

reader@ubuntu:~/scripts/chapter_08$ vim hello-int.sh 
reader@ubuntu:~/scripts/chapter_08$ cat hello-int.sh
#/bin/bash

# Assign a number to the variable.
hello_int=1

echo $(( ${hello_int} + 1 ))

reader@ubuntu:~/scripts/chapter_08$ bash hello-int.sh
2

By including the variable + 1 inside $((...)), we tell Bash to evaluate it as arithmetic.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.226.251.70