Introducing variables
Think of variables as name tags or containers that store data. You can assign any data, such as text, numbers, filenames, and more, to a short and memorable variable name. Throughout your script, you can repeatedly refer to the data by its given variable name and make decisions on the data or even change the data the variable refers to. Technically, a variable is a declaration that allocates memory storage space and assigns it a value.
In the following subsections, we’ll be breaking the subject of variables into bite-sized chunks to make it easier to digest.
Declaring variables
To declare a variable in Bash, you simply assign a value to a variable name. The syntax for declaring a variable is as follows:
variable_name=value
For example, to declare a variable named my_variable
with a value of Hello, World!
, you would use the following command:
my_variable="Hello, World!"
Important note
There should be no spaces around the equals...