Showing posts with label variables. Show all posts
Showing posts with label variables. Show all posts
Friday, May 5, 2017
Bash variables type
Bash variables type
Unlike many other programming languages, Bash does not separate its variables by "type". Essentially, Bash variables are character strings, but, depending on context, Bash permits integer operations and comparisons on variables. The determining factor is whether the value of a variable contains only digits.
Integer or string?
a=2334
# Integer.
let "a += 1"
echo "a = $a "
# a = 2335
# Integer, still.
b=${a/23/BB}
# Substitute "BB" for "23".
# This transforms $b into a string.
echo "b = $b"
# b = BB35
declare -i b
# Declaring it an integer doesnt help.
echo "b = $b"
# b = BB35
let "b += 1"
# BB35 + 1 =
echo "b = $b"
# b = 1
c=BB34
echo "c = $c"
# c = BB34
d=${c/BB/23}
# Substitute "23" for "BB".
# This makes $d an integer.
echo "d = $d"
# d = 2334
let "d += 1"
# 2334 + 1 =
echo "d = $d"
# d = 2335
# What about null variables?
e=""
echo "e = $e"
# e =
let "e += 1"
# Arithmetic operations allowed on a null variable?
echo "e = $e"
# e = 1
# Null variable transformed into an integer.
# What about undeclared variables?
echo "f = $f"
# f =
let "f += 1"
# Arithmetic operations allowed?
echo "f = $f"
# f = 1
# Undeclared variable transformed into an integer.
The burden is on the programmer to keep track of what type the script variables are.
Bash will not do it for you.
But declare or typeset built-ins permit restring the properties of variables.
This is very weak form of the typing available in certain programming languages.
Ex:1
declare i number
# The script will treat subsequent occurrences of number as an integer
number=3
echo Number = $number
# Number = 3
number=three
echo Number = $number
#Number = 0
#Tries to evaluate the string three as an integer
Available link for download
Monday, April 24, 2017
Bash variables scope
Bash variables scope
Bash variables are defaults to global
What makes a variable local?
- A variable declared as local is one that is visible only within the block of code in which it appears. It has local "scope." In a function, a local variable has meaning only within that function block.
Ex:
local variable name
func ()
{
local loc_var=23
# Declared as local variable.
# Uses the local builtin.
echo ""loc_var" in function = $loc_var"
global_var=999
# Defaults to global.
echo ""global_var" in function = $global_var"
}
func
# Now, to see if local variable "loc_var" exists outside function.
echo ""loc_var" outside function = $loc_var"
# $loc_var outside function =
# No, $loc_var not visible globally.
echo ""global_var" outside function = $global_var"
# $global_var outside function = 999
# $global_var is visible globally.
exit 0
- Before a function is called, all variables declared within the function are invisible outside the body of the function, not just those explicitly declared as local
func ()
{
global_var=37
# before the function has been called.
} # END OF FUNCTION
echo "global_var = $global_var"
# global_var =
# Function "func" has not yet been called,
# so $global_var is not visible here.
func
echo "global_var = $global_var"
# global_var = 37 Has been set by function call.
- Using the declare builtin restricts the scope of a variable
foo ()
{
FOO="bar"
}
bar ()
{
foo
echo $FOO
}
bar
# Prints bar
However . . .
foo (){
declare FOO="bar"
}
bar ()
{
foo
echo $FOO
}
bar
# Prints nothing.
Available link for download
Subscribe to:
Posts (Atom)