Bash

Bash Variable

Bash Variable

Variables are the essential part of programming, or we will say that they're the spirit of a programing language. Variables specify the memory location through characters, numeric, and alphanumeric. they're wont to be referenced and manipulated during a computer virus.

What are the variables?

Variables are the containers that store data or useful pieces of info because of the value inside them. Below is that the syntax for a variable:

Variable_name =value 

A Variable may be a combined sort of two words, i.e., vary + able, which suggests its value are often changed, and it is often used multiple times.

Variable is understood because of the temporary storage for any quiet data like integer, float, char, etc. A variable name can include alphabets, digits, and underscore, and its name is often started with alphabets and underscore only.

Note: A variable name cannot start with a digit.

What are Bash Variables?

We cannot utilize a bash variable without the right data about them, data such as syntax, data types, types, working. so, let's go throughout this brief tutorial for having an acceptable overview of Bash Variables.

At first, know the syntax.

Syntax: 

Variable_name=value

Some rules to keep in mind while declaring a bash Variable:

Prefix the variable name with the dollar ($) sign while reading or printing a variable.

Do not add a dollar sign ($) while creating a variable with any value.

A variable name could also be alphanumeric, or it's going to be written with an underscore (_).

A variable name is case-sensitive so we should not change the variable name otherwise it will throw an error. For instance x and X are considered as two different variables.

a variable name is often written either in UPPER_CASE or LOWER_CASE letters or a mix of both as you would like.

A variable is often placed anywhere during a Bash Script or on the instruction, because on runtime, Bash will replace it with its assigned value. It became possible due to doing substitution before running the command.

There shouldn't be whitespace on either side of the equal sign (=) between the variable name and its value. Given are some instances of Unreasonable Variables possessing whitespaces (denoted by dots ...) between them as we can see here:

var1=...variable1

var2...=variable2

var3...=...variable3

There is no need of using any quotes, either single or double, to define a variable with one character value like var1=variable. To input multiple words or String as one item during a variable, then make use of quotes for enclosing your content therein variable.

Single Quote ('') assists to manage every character.

Double Quote ("") helps to try to the substitution.  

Data Types

When it comes to datatypes Bash is similar to Python, Similarly as we don’t need to assign any datatypes to the variables. Bash shell automatically identifies the datatype the moment we assign them.

For example, when we assign a integer to a variable it will automatically understand the datatype and give us the output.

Types of Bash Variables

In a Linux system only two types of variables can be used,

  1. System defined variables
  2. User defined variables

System defined variables: -  In Bash Shell, there are some in built variables which comes with the Operating system. Their standard convention is that generally they're defined in capital letters, i.e., UPPER_CASE. So whenever you see a variable defined in upper cases, presumably , they're the system-defined variables.

 Here are some system defined variable:

  1. BASH
  2. BASH_VERSION
  3. COLUMNS
  4. HOME
  5. LOGNAME
  6. OSTYPE
  7. PWD
  8. USERNAME

Here is the script using the system Variables

#! /bin/bash  
 
# For now we will only use the echo command
<< COMMENT o="Bash shell scripts can be very handy if we use it nicely :) "  
echo "$o"
COMMENT
echo "$BASH"
echo "$BASH_VERSION "
echo "$COLUMNS" 
    
echo    "$OSTYPE"
echo    "$PWD"

Output:
Graphical user interface, text, application, website

Description automatically generated

User Defined  Variables:

The Variables which is being created by the use is called user defined variables.

Given is the code of a script which is created by using the USER_DEFINED_VARIABLE.

#! /bin/bash  
 
# For now we will only use the echo command
 
name="Greate Learning Academy"
Student="WElcomes you to their platform!"
work="Happy learning!"
echo "$name $Student $work"

Output: