--- layout: page category-page: scripts category-title: Scripting tags: variables, defining, deleting, naming author: Dario Rasic title: Script Variables ---

A variable is simply a string to which we assign a certain type of data, which could be a text, a number, a filename and other types of data.

Naming a variable

To name a variable in Unix we have to use only letters, numbers or the underscore character (_).
Other characters can't be used because they have a special meaning in Unix Shell.

Some simple examples are:
  VAR_1
  VAR_2
  NAME_3
  name_4

Defining a variable

To define a certain variable, we could use the following basecase:
  variable_name=variable_value
Let me show you a simple example:
  VAR_1=Strawberry
To access a variable we have to use the dollar sign ($). So if I want to access VAR_1, I have to write:
  VAR_1=Strawberry
  echo $VAR_1
And shell will give us the following result:
  Strawberry

Deleting a variable

Deleting a variable means that shell will remove a certain variable from the list of those that it tracks.
To delete a variable we use the following command:
  unset variable_name
which in our case would be:
  unset VAR_1

Protecting variables

To protect a certain variable, we can set them as read-only so that it can't be changed or deleted.
So, if we try to change the value of VAR_1, the result will be the following:
  VAR_1=Strawberry
  readonly VAR_1
  VAR_1=Blueberry
which will give us:
  VAR_1: This variable is read only.
If we try to delete the variable, shell will give us the following value:
  VAR_1=Strawberry
  unset  VAR_1
  echo $VAR_1
As VAR_1 is read-only, shell will not give us any output, as you can't use the unset command with read-only files.