Since I'm mainly a C* programmer (C, C++ and C#), my shellscript looks too much like a common program, admittedly. This means that I use a lot of functions. But there's a catch: variables used in bash are automatically global. So, if I execute the portion of code below:
#!/bin/bash
function test()
{
value=123
echo $value
}
value=321
echo $value
test
echo $value
function test()
{
value=123
echo $value
}
value=321
echo $value
test
echo $value
I will get the following output:
321
123
123
123
123
Which isn't what I intended. So, I found this nice command that was secret to me, called "local". Variables in bash can be declared, and it's scope can be determined by using this command. So, the following script:
#!/bin/bash
function test()
{
local value=123
echo $value
}
value=321
echo $value
test
echo $value
function test()
{
local value=123
echo $value
}
value=321
echo $value
test
echo $value
Which would produce:
321
123
321
123
321
Which would be exactly what I desired.