Unofficial PlayOnLinux Wiki
Previous chapter: Talking with the user

Introduction[]

Okay, so what is a variable? If you've done some math (and can remember anything from it) you might already have dealt with variables, like x = 5 - y. Although, in programming are variables used for more than numbers. We can also do like this:

bar="Some random text"
# A piece of text is called a "string".

Or maybe something like this:

foo=true
# Saving either true or false (without quotes) makes the variable a "boolean".

New to variables[]

If you never have used a variable before you might need a better explanation. You can think of a variable as a box, but a box you only can put ONE thing in. When you put a new thing in the box the thing which already was in the box (if there were any) gets destroyed. Here is an example:

My_Variable="Hello!"
# We save the string "Hello!" to the variable called "My_Varible".

Another_Variable="Good day"
# We save the string "Good day" to the variable called "Another_Variable"

My_Variable="Hi!"
# We save the string "Hi!" to the variable called "My_Variable" 
# AND OVERWRITES THE OLD VALUE!
# Now our first "Hello!" is gone for ever!

Sometimes you have to use variables, for example if you want to save an answer from the user.

BASH's Syntax[]

Okay, lets be honest already. The syntax in Bash is awful! You have to write perfectly (a space can destroy your code) and also very weird. But lets get through the most important things about variables now!

Assigning a variable[]

It is rather easy to make a new variable. This is the code:

My_Variable1="Hello, what are you doing?" # Make a string
My_variable2=42 # Make a number.
My_variable3=false # Make a boolean.

What to think about:

  • You can not use any spaces around the equal-sign, this will cause an error.
  • The variables name should just contain underscores, letters and numbers. It is a good habit to avoid any acute accents (`) or any special-letters for your language (Ü, Å, µ, 日).
  • Always use quotes around strings! You can skip them if it is only one word in your string, but it is a good habit to always use them!
  • Any special characters in your string should be escape'd! Escaping is when you add a back-slash before the character. Example:
    String_Variable="Please press \"Enter\" or any other key."

Using a variable[]

When you want to use a variable you must think a little harder. Lets start right of with an example!

My_Text="Welcome to my cool script, please be have patience while I learn how to script!"
My_Title="Welcome!"
# Create the variables

POL_SetupWindow_Init
POL_SetupWindow_message "$My_Text" "$My_Title"
POL_SetupWindow_Close
# Creates a window which shows some text and closes on a "Forward" press.

As you can see I am using $Variable_Name to use a variable. When Bash sees the dollar-sign it thinks: "Alright, this must be a variable!" and exchanges the variable for the variables value. The code above could just as well been written like this:

POL_SetupWindow_Init
POL_SetupWindow_message "Welcome to my cool script, please be have patience while I learn how to script!" "Welcome!"
POL_SetupWindow_Close

Variables can also be defined as the value another variable has. Take a look:

foo="Hello"
# Give the value "Hello" to foo.

bar="$foo"
# Give the value of foo to bar. ("Hello")

baz="$foo World"
# Give the value "Hello World" to baz.

What to think about:

  • Never forget to use quotes around a variable! As you will see later, it is very important to have that!
  • Lets say you want to make something like this:
foo="dance"
bar="he $foos all day long"
# bar = "He dances all day long"

This will NOT work! Bash, will search for a variable "foos", which is not defined! Instead you must use the longer version:

foo="dance"
bar="He ${foo}s all day long"
# b="He dances all day long"

Try it![]

Now lets give it a try! Open up a terminal and use what you've learnt, the variables will not be saved when you close the terminal. Have fun!

Previous chapter: Talking with the user