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

Bash Shell Variable

Variables form the fundamental building blocks of any programming language. Shell and bash script programming offer variables, much like other languages.

A variable serves as a container used for storing data in programming. It includes a pointer to the memory location of the data.

Declare variable: To create a variable, you must assign a value to it.

variableName=VariableValue

variableName:

It is the name of the variable, which can contain any combination of alphabets (a to z, A to Z), numbers (0 to 9), and underscores (_).

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

For example

AGE=25

A variable named AGE is created and assigned the value 25.

How to Access Variables in Bash

After declaring and assigning values to variables, you can access them using a dollar symbol ($) followed by the variable name.

AGE=25
echo $AGE

This above code declares a variable named AGE with a value of 25 and then uses echo to display the value of the AGE variable. The dollar symbol before the variable name is crucial for accessing its value.

Bash Shell Readonly Variables

Once variables are assigned values, you can change them to new values using the assignment operator =.

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

Output:

25
35

How do you make variables not updatable?

The readonly keyword prevents a variable from being updated, effectively turning it into a constant.

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

AGE is an constaints, assigning new value throws an error , and the error message is AGE: is read-only.

Output:

25
 line 6: AGE: is read-only

Bash unset variable

The unset keyword assists in removing the value from the specified variable. The variable remains accessible but prints an empty value.

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

Output:

25
empty:

Above code,

  • first sets the variable AGE to 25, prints its value,
  • then unsets it using unset AGE.
  • Subsequently, it prints “empty” followed by the value of AGE, which now appears as empty space.

Variable Scopes

Every declared variable must have a scope, defining where in the program the variable can be used.

For instance, if a variable is declared within a function, it is available only within that function and is not accessible outside of it.

Variable scopes in Bash can be defined in two ways

  • Global variable
  • Local variable:

Bash Global Variables

Variables declared in a shell script are referred to as global variables.

Global variables can be accessed within a function or any nested blocks of a shell script file.

The default variable declared in a script file 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 variables is visible only within the block where they are declared.

Syntax:

local variablename=variablevalue

In this syntax, 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 is only visible within that function. Variables declared outside functions are called global variables and are available for all functions.

Variables typing

Bash scripting is not typed language, however you can declare an variable with a type of the data using declare command Based on the type of variable, it allows the type of the data .

declare options variablename=value

variable is declared and assigned with a value.

Options contains option to create a type of a varialbe

Array: To create an array variable

declare -a variable=

Variable TypeSyntaxDescription
Arraydeclare -a variabledeclare an indexedarray variable that stores strings
Associated Arraydeclare -A variableAssociated Array
Integerdeclare -i variablenumeric value to store in variable
Readonlydeclare -r variablereadonly variable, cannot be changed or unset
Exportdeclare -x variableexport the variable and used by all child process

Display Environment variables in Bash

In Bash, there are two types of commands for printing environment variables.

  • The printenv command
  • The env command

Both of these commands list all the environment variables of the terminal.

Variable nameing convention

  • Variables are read by prefixing with the $ symbol.
  • Variable names are composed of alphabets, numbers, or underscores.
  • Variables are case-sensitive; for example, test and Test are considered two different variables in scripting.
  • While variable names are conventionally written in UPPERCASE, you can create them using UPPER or LOWER case if needed. And Environment and Shell variables are both in UPPERCASE.
  • Variable names can not contain spaces
  • Names conventinaly should be camelCase. Example firstName

Shell variables

Shell variables are variables are set by the shell, not by the user. These are required by shell to work smoothly

VariableDescription
PWDCurrent working directory
Set-LocationChange the working directory to new directory
Rename-ItemRename a file
IFSInternal FIeld Separator by default is space, set by Shell, Used for string split
PATHContains semicolon separated path of commands, Configured to lookup for commands
UIDPrints the User Identifcation number
HomeHome directory of a current user