Variables

Think of a variable as a named bucket that we can place data into. We can then use these variables (or, more practically, their contents) within our code.

To create, or Declare a variable, simply enter its name followed by an = with no spaces, and then the value of the variable (again with no spaces):

Declaring Variables
example_variable=Windows
example_variable2=Sucks

Pro-Tip: BE CONSISTENT WITH YOUR NAMING SCHEMA

We can then reference variables with a $ sign:

Calling Variables
echo $example_variable $example_variable2
Windows Sucks

Now, there are a few tripping points here. Consider the following:

example=Hello World
bash: World: command not found

Because there was a space in the variable value, bash tried to interpret it, viewing World as a command. To get around this, we have to encapsulate our input in either ' ' or " ", but there are differences between them...

  • ‘ ’ - The contents between them are literal, including special characters

  • “ ” - The contents between them are literal, but certain special characters are still interpreted, such as $, \, and `

Examples of Encapsulation
greeting='Hello World'
echo $greeting
Hello World

greeting2="New $greeting"
echo $greeting2
New Hello World

Last updated