2018-11-14 10:55:05 +00:00
|
|
|
---
|
|
|
|
layout: page
|
|
|
|
category-page: scripts
|
|
|
|
category-title: Scripting
|
2018-11-15 20:09:21 +00:00
|
|
|
tags: variables defining deleting naming
|
2018-11-14 10:55:05 +00:00
|
|
|
author: Dario Rasic
|
|
|
|
title: Script Variables
|
2018-11-18 20:38:56 +00:00
|
|
|
previous-page: pages/scripts/0-base-commands.html
|
|
|
|
next-page: pages/scripts/2-special-variables.html
|
2018-11-14 10:55:05 +00:00
|
|
|
---
|
|
|
|
<!-- Intro -->
|
2018-11-14 21:15:38 +00:00
|
|
|
A variable is simply a string to which we assign a certain type of data,
|
2018-11-19 15:05:42 +00:00
|
|
|
which could be some text, a number, a filename or other types of data.<br>
|
2018-11-15 20:09:21 +00:00
|
|
|
|
2018-11-14 10:55:05 +00:00
|
|
|
<!-- How to name a variable - text -->
|
|
|
|
<h3>Naming a variable</h3>
|
2018-11-15 20:09:21 +00:00
|
|
|
|
2018-11-14 10:55:05 +00:00
|
|
|
<!-- Explaination -->
|
2018-11-19 15:05:42 +00:00
|
|
|
To name a variable in Unix we can only use letters, numbers or
|
2018-11-14 21:15:38 +00:00
|
|
|
the underscore character (_).<br>
|
2018-11-14 10:55:05 +00:00
|
|
|
Other characters can't be used because they have a special meaning in Unix Shell.<br>
|
2018-11-15 20:09:21 +00:00
|
|
|
|
2018-11-14 10:55:05 +00:00
|
|
|
Some simple examples are:
|
|
|
|
|
|
|
|
<pre>
|
2018-11-15 20:09:21 +00:00
|
|
|
VAR_1
|
|
|
|
VAR_2
|
|
|
|
NAME_3
|
|
|
|
name_4
|
2018-11-14 10:55:05 +00:00
|
|
|
</pre>
|
|
|
|
|
|
|
|
<!-- How to define a variable - text -->
|
|
|
|
<h3>Defining a variable</h3>
|
|
|
|
|
2018-11-19 15:05:42 +00:00
|
|
|
To define a certain variable, we use the following base case:
|
2018-11-14 10:55:05 +00:00
|
|
|
|
|
|
|
<pre>
|
2018-11-15 20:09:21 +00:00
|
|
|
variable_name=variable_value
|
2018-11-14 10:55:05 +00:00
|
|
|
</pre>
|
2018-11-15 20:09:21 +00:00
|
|
|
|
2018-11-14 10:55:05 +00:00
|
|
|
<!-- Examples of defining -->
|
|
|
|
Let me show you a simple example:
|
|
|
|
|
|
|
|
<pre>
|
2018-11-15 20:09:21 +00:00
|
|
|
VAR_1=Strawberry
|
2018-11-14 10:55:05 +00:00
|
|
|
</pre>
|
2018-11-15 20:09:21 +00:00
|
|
|
|
2018-11-14 10:55:05 +00:00
|
|
|
<!-- How to access the variables - text -->
|
2018-11-14 21:15:38 +00:00
|
|
|
To access a variable we have to use the dollar sign ($). So if I want to
|
|
|
|
access VAR_1, I have to write:
|
2018-11-14 10:55:05 +00:00
|
|
|
<!-- Examples of accessing -->
|
|
|
|
<pre>
|
2018-11-15 20:09:21 +00:00
|
|
|
VAR_1="Strawberry"
|
|
|
|
echo $VAR_1
|
|
|
|
Strawberry
|
2018-11-14 10:55:05 +00:00
|
|
|
</pre>
|
2018-11-15 20:09:21 +00:00
|
|
|
|
2018-11-14 10:55:05 +00:00
|
|
|
<!-- How to delete a variable - text -->
|
|
|
|
<h3>Deleting a variable</h3>
|
2018-11-15 20:09:21 +00:00
|
|
|
|
2018-11-19 15:05:42 +00:00
|
|
|
Deleting a variable means that the shell will remove a certain variable from the list of
|
2018-11-14 21:15:38 +00:00
|
|
|
those that it tracks.<br>
|
2018-11-14 10:55:05 +00:00
|
|
|
To delete a variable we use the following command:
|
2018-11-15 20:09:21 +00:00
|
|
|
|
2018-11-14 10:55:05 +00:00
|
|
|
<pre>
|
2018-11-15 20:09:21 +00:00
|
|
|
unset variable_name
|
2018-11-14 10:55:05 +00:00
|
|
|
</pre>
|
|
|
|
|
|
|
|
which in our case would be:
|
|
|
|
|
|
|
|
<pre>
|
2018-11-15 20:09:21 +00:00
|
|
|
unset VAR_1
|
2018-11-14 10:55:05 +00:00
|
|
|
</pre>
|
2018-11-15 20:09:21 +00:00
|
|
|
|
|
|
|
<!-- How to protect a variable - text -->
|
2018-11-14 10:55:05 +00:00
|
|
|
<h3>Protecting variables</h3>
|
2018-11-15 20:09:21 +00:00
|
|
|
|
2018-11-19 15:05:42 +00:00
|
|
|
To protect a certain variable, we can set it as read-only so that it can't be
|
2018-11-14 21:15:38 +00:00
|
|
|
changed or deleted.<br>
|
2018-11-14 10:55:05 +00:00
|
|
|
So, if we try to change the value of VAR_1, the result will be the following:
|
|
|
|
|
|
|
|
<pre>
|
2018-11-15 20:09:21 +00:00
|
|
|
VAR_1="Strawberry"
|
|
|
|
readonly VAR_1
|
|
|
|
VAR_1="Blueberry"
|
|
|
|
VAR_1: This variable is read only.
|
2018-11-14 10:55:05 +00:00
|
|
|
</pre>
|
|
|
|
|
2018-11-19 15:05:42 +00:00
|
|
|
If we try to delete the variable, the shell will give us the following error:
|
2018-11-14 10:55:05 +00:00
|
|
|
|
|
|
|
<pre>
|
2018-11-15 20:09:21 +00:00
|
|
|
VAR_1="Strawberry"
|
|
|
|
unset VAR_1
|
|
|
|
VAR_1: This variable is read only.
|
2018-11-14 10:55:05 +00:00
|
|
|
</pre>
|