Understanding and Writing ‘Linux Variables’ in Shell Scripting – Part 10

[‘

n

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.

n

Understanding Variables in Shell Scripting
Understanding Variables in Shell Scripting

n

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.

n

Variable Attributes

n

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”.

n

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.

n

Declare a variable Integer, bill = 121

n

[emailxa0protected]:~$ declare -i bill=121

n

Printf the value of variable bill.

n

[emailxa0protected]:~$ printf "%d\n" "$bill" rnrn121

n

Let the variable value be a string. The variable bill is already declared, no need to declare it second time. Just change the value of variable as.

n

[emailxa0protected]:~$ bill=tecmint

n

Now, again printf the value of variable bill.

n

[emailxa0protected]:~$ printf "%d\n" "$bill" rnrn0

n

Notice ‘0‘ in the place of error message.

n

Now, the question is, how to know the attributes of already declared variables?

n

[emailxa0protected]:~$ declare -p bill rnrndeclare -i bill="121"

n

Here, a -p (stands for print) switch comes to rescue.

n

Again, what should I do to switch off the attributes of a variable?

n

In order to switch Off the attributes of a variable what we need all is to put a + (plus) sign just before the switch. Here it is more clear from the example below.

n

Switch Off the integer attribute for the above variable.

n

[emailxa0protected]:~$ declare +i bill

n

Check the value of the variable.

n

[emailxa0protected]:~$ printf "%d\n" "$bill" rnrnbash: printf: bill: invalid numberrn0

n

Now print the value of variable using switch string.

n

[emailxa0protected]:~$ printf "%s\n" "$bill" rnrntecmint

n

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.

n

Read-only Variables

n

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

n

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.

n

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

n

[emailxa0protected]:~$ declare -r name="Tecmint.com"

n

Printf the value of above declared variable.

n

[emailxa0protected]:~$ printf "%s\n" "$name" rnrnTecmint.com

n

Try to change the value of variable.

n

[emailxa0protected]:~$ declare -r name="Avishek" rnrnbash: declare: name: readonly variable

n

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

n

Exporting Variables in Linux

n

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.

n

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.

n

[emailxa0protected]:~$ declare -x variable=”Constant_Value”

n

All the changes made to the variable while the script is running, is lost when the variable is exported outside of the script. Exporting variable is very important in shell scripting.

n

We want to have a variable that should be read-only and available outside of the script, we need to use switch -r and switch -x at the same time.

n

[emailxa0protected]:~$ declare -rx variable=”Constant_Value”

n

Environment Variables

n

The variables that are shared between the program and the program that execute them. Environment variables can be exported but the attributes can’t be assigned to it.

n

Understanding the above theory practically. Here we have two scripts 0.sh and 1.sh.

n

# 0.shrn#!/bin/bash rndeclare -rx name=Tecmint rnbash 0.sh rnexit 0

n

And the second script is.

n

# 1.shrn#!/bin/bash rnprintf "%s\n" "$name" rnname=Tecmint.com rnprintf "%s\n" "$name"rnexit 0

n

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

n

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‘.

n

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.”

n

References

n

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

n

    n

  1. -a : Declares an array.
  2. n

  3. -f : Display Function and Definition.
  4. n

  5. -F : Display Function Name.
  6. n

  7. -r : Declare variable as read-only.
  8. n

  9. -x : Declare Variable as Exportable.
  10. n

  11. -I : Declare variable as Integer.
  12. n

n

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.

n

‘]