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

Bash Shell Variable

variable are basic blocks of any programming language. Shell and bash script programming provide 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 a 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 in Bash

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 a new assignment.

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

Output:

25
35

How do you make variables not updatable?

The readonly keyword does not allow a change in 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, the error is `AGE: is read-only. Output;

25
 line 6: AGE: is read-only

Bash unset variable

unset keyword helps to remove the value from the given variable. The Variable is still accessible and prints an 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

Bash 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 declared is called a global variable

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

Output

Inside Function Age: 40
Script Age: 40

Bash 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, the variable is declared and assigned with the 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 in Bash

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