The Linux shell scripting language has always been a hot topic and will always be in the future. The Shell Scripting Language is magical and its so easy to do a program in scripting language as in any other Language. However, it needs an in-depth knowledge of what we are doing and what the result is expected.

Understanding Variables in Shell Scripting

All the shell scripting articles we have written for our readers are highly appreciated including the last one “An Insight of Linux Variables”. We are extending the last article to a new Level.

Variable Attributes

Each Variable in Linux Environment has certain options, and those are called ‘attributes’. These options or attributes can be turned On and Off, when required according to the situation using a command “declare”.

An Example of variable attribute is using a switch say ‘-i‘ which will turn on the integer attribute for the corresponding variable. Even if a non-numeric value is passed to switch ‘-i‘ it won’t throw an error message and output ‘0‘ as integer interpretation. Here it becomes more clear from the example below.

Here in the above example, bash could not judge non-numeric value as error, however printf is interpreting, what could be a number and what could not be.

Read-only Variables

You might have heard Read Only Memory (ROM), but what is Read-only Variable? Has it any similarity with ROM?

Well Read-only Variables like Read-only Memory is something the value of which can’t be change once it is assigned. Hence it is called Read-only. You can’t write, edit or modify a new value for that variable. Here is an illustration using example.

Decalre a read only (-r) variable name, the value of which is “Tecmint.com”.

As discussed above the attributes of a read-only Variable can be changed using ‘+‘ sign.

Exporting Variables in Linux

All the shell variables declared in a shell script are available till the script is running. Outside of the script the variable from the script does not exist. The process of making variables available outside of the script is called exporting variables.

A Variable can be exported outside of the shell using switch declare -x (export), which acknowledge the shell what you wanted to export. A declare export switch can be used as.

Here what is going on, is a variable (name) is declared as read only and exported and immediately after that second script is called.

The second script just printed the variable from the first script that was exported in the first printf statement. In the second printf statement it shows the new value assigned to variable ‘name‘.

No need to worry, that the variable was read-only, how can it be reassigned. Don’t you remember that “All the changes made to the variable while the script is running, is lost when the variable is exported outside of the script.”

References

The declare command allows all the switches below along with their combination.

-a : Declares an array.

-f : Display Function and Definition.

-F : Display Function Name.

-r : Declare variable as read-only.

-x : Declare Variable as Exportable.

-I : Declare variable as Integer.

That’s all for now. In the very next article we will be discussing ways to substitute variables using ‘eval‘ command and the variables already defined in bash before closing this topic. Hope you people are enjoying your journey to in-depth of scripting. Till then stay tuned and connected to Tecmint.com.