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

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

Declare a variable Integer, bill = 121

avi@localhost:~$ declare -i bill=121

Printf the value of variable bill.

avi@localhost:~$ printf "%d\n" "$bill" 

121

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.

avi@localhost:~$ bill=tecmint

Now, again printf the value of variable bill.

avi@localhost:~$ printf "%d\n" "$bill" 

0

Notice ‘0‘ in the place of error message.

Now, the question is, how to know the attributes of already declared variables?
avi@localhost:~$ declare -p bill 

declare -i bill="121"

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

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

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.

Switch Off the integer attribute for the above variable.

avi@localhost:~$ declare +i bill

Check the value of the variable.

avi@localhost:~$ printf "%d\n" "$bill" 

bash: printf: bill: invalid number
0

Now print the value of variable using switch string.

avi@localhost:~$ printf "%s\n" "$bill" 

tecmint

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

avi@localhost:~$ declare -r name="Tecmint.com"

Printf the value of above declared variable.

avi@localhost:~$ printf "%s\n" "$name" 

Tecmint.com

Try to change the value of variable.

avi@localhost:~$ declare -r name="Avishek" 

bash: declare: name: readonly variable

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.

avi@localhost:~$ declare -x variable=”Constant_Value”

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.

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.

avi@localhost:~$ declare -rx variable=”Constant_Value”

Environment Variables

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.

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

# 0.sh
#!/bin/bash 
declare -rx name=Tecmint 
bash 0.sh 
exit 0

And the second script is.

# 1.sh
#!/bin/bash 
printf "%s\n" "$name" 
name=Tecmint.com 
printf "%s\n" "$name"
exit 0

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.

  1. -a : Declares an array.
  2. -f : Display Function and Definition.
  3. -F : Display Function Name.
  4. -r : Declare variable as read-only.
  5. -x : Declare Variable as Exportable.
  6. -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.

If you read this far, tweet to the author to show them you care. Tweet a thanks
Ronav Saive
A Passionate GNU/Linux Enthusiast and Software Developer with over a decade in the field of Linux and Open Source technologies.

Each tutorial at TecMint is created by a team of experienced Linux system administrators so that it meets our high-quality standards.

Join the TecMint Weekly Newsletter (More Than 156,129 Linux Enthusiasts Have Subscribed)
Was this article helpful? Please add a comment or buy me a coffee to show your appreciation.

2 thoughts on “Understanding and Writing ‘Linux Variables’ in Shell Scripting – Part 10”

Got something to say? Join the discussion.

Thank you for taking the time to share your thoughts with us. We appreciate your decision to leave a comment and value your contribution to the discussion. It's important to note that we moderate all comments in accordance with our comment policy to ensure a respectful and constructive conversation.

Rest assured that your email address will remain private and will not be published or shared with anyone. We prioritize the privacy and security of our users.