This post is about how to bash commands and programming tutorials with examples.

Shell Variable

variables are basic blocks of any programming language. Shell and bash scripts programming provides variables like others.

It is a container used to store the data in programming. It contains a pointer to the memory location of the data. Declare variable To create a variable, You must assign the value.

variableName=variableValue

variableName

It is the name of the variable that contains any names by using a combination of alphabets(a to z, A to Z), numbers(0 to 9), and underscore(_) symbols.

VariableValue is a value stored in a variable, It can be a string of numbers or boolean. equal symbol(=) is to assign the value to a variable.

For example

AGE=25

variable=AGE is created and assigned with value=25

how to access variables?

Once variables are declared and assigned value, You can access them with a prefix dollar symbol($)+ variable name

AGE=25
echo $AGE

Bash Shell Readonly variables

variables are changed to new values using new assignment

AGE=25
echo $AGE
AGE=35
echo $AGE

Output:

25
35

How do you make variables not updatable?

readonly keyword does not allow change its value. These are also called constants.

AGE=25
echo $AGE
readonly AGE
AGE=35
echo $AGE

The above code throws an error during AGE=35, error is AGE: is read only. Output;

25
 line 6: AGE: is read only

unset variable

unset keyword helps to remove the value from the given variable. Variable is still accessible and prints empty value.

AGE=25
echo $AGE
unset AGE
echo "empty":$AGE

Output:

25
empty:

You can define variable scopes in two ways

  • Global variable
  • Local variable

Global Variables

Variables declared in a shell script are called global variables.

global variables accessed within a function or any nested blocks of a shell script file

default variable declare is called global variable

setAge() {
    echo "Inside Function Age: $AGE"
}
AGE=40
setAge
echo "Script Age: $tmp"

Output

Inside Function Age: 40
Script Age: 40

Local variables

Local variables are declared inside a block of code or a function. The scope of these variable types is visible inside it only.

Syntax:

local variablename=variablevalue

Here, variable is declared and assigned with local keyword

setAge() {
    local AGE=25
    echo "Local Variable Age: $AGE"
}
AGE=40
setAge
echo "Global Age: $tmp"

Output

Local Variable Age: 25
Global Age: 40

a local variable is declared inside a function and only visible inside a function. The outside function is called global variables that are available for all functions.

Display Environment variables

There are two types of commands to print environment variables.

  • printenv command
  • env command

Both the above commands list out all environment variables of the terminal.

Important points about variables

  • variable read by prefix with $ symbol
  • variable names are written using alphabets or numbers or underscore
  • variables are case sensitive. for example, test and Test are two different variables in scripting
  • variable names are UPPERCASE by convention, but you can create using UPPER or LOWER case