Saturday 19 November 2016

Shell Scripting Tutorial Variables Global and Local Variables


In Shell Scripting we will use Variables to store some values. Variables are more useful when you have repeated values you would like to use in script multiple times. Instead of writing same values multiple times in shell script  we can assign one variable value and call same variable in script multiple times. In this article we are going to see Shell Scripting Tutorial Variables Global Variables and Local Variables.

Rules to Define Variables in Shell Scripting

  • Variable Must be Start/Begin with Letter
  • In Variable space are not allowed
  • Instead of spaces we can make use of under_scores
  • Special Characters Like @ # are not allowed
  • Do not use Hyphen (-)
  • Do not Start Variable with Number
  • To Pass Commands to Variable use $(Command) Or `Command`
  • Define Local variable using local in front of variable (.i.e. local variable)
  • Variable must be defined starting for the script, If not local variable.
  • Variable are case sensitive

Let’s see few Examples about Variables Global and Local Variables

Below are few different types of variables defined as per above rules, Let’s see what happens when we execute below variable script.
#!/bin/bash
##Purpose: Define and execute Variable. Few Examples.
##Date: 19th Oct 2016
##Author: Ankam Ravi Kumar
##WebSite: https://arkit.co.in

##Start

## GLOBAL VARIABLE
A=10
B=22
HOST=$(hostname)
HOST1=`hostname`
1value=222
false@var=false
hyphen-a=wrong

echo "Variable A Value: $A"
echo "Variable B Vaule: $B"
echo "Variable HOST value: $HOST"
echo "Variable HOST1 value: $HOST1"
echo "Wrong Variable 1value $1value"
echo 'False @ Variable' $false@var
echo "hyphen-a Variable Value: $hyphen-a"

##END
Shell Scripting Variables
ravi@TechTutorials:/script$ sh variables.sh
variables.sh: 14: variables.sh: 1value=222: not found
variables.sh: 15: variables.sh: false@var=false: not found
variables.sh: 16: variables.sh: hyphen-a=wrong: not found
Variable A Value: 10
Variable B Vaule: 22
Variable HOST value: TechTutorials
Variable HOST1 value: TechTutorials
Wrong Variable 1value value
False @ Variable @var
hyphen-a Variable Value: -a
Shell Script Variables Executed Output
If you can see above shell script executed, It is showing an errors where variable rules are not matching.
 14 1value=222
 15 false@var=false
 16 hyphen-a=wrong
Rule Says do to start variable with numbers, Do not use special symbols in variable and do not use hyphen -. So they are giving an errors.

Case sensitive Variable Examples

Whenever we are writing Variable we have to be careful if you write upper case and lower case each and every character will differ because it is case sensitive.
NOTE: Best practice is to write shell script variable in UPPER case.
#!/bin/bash
## Variable is Case sensitive
## Author: Ankam Ravi Kumar
## WebSite: https://arkit.co.in

## START
NAME=Ravi
NaMe=Kumar
nAmE=arkit

echo "NAME Variable: $NAME"
echo "NaMe Variable: $NaMe"
echo "nAmE Variable $nAmE"

## END
http://computertechnologyspecial.com/

No comments:

Post a Comment