Assignment Operators in Bash

In the Bash shell, assignment operators are used to assign values to variables. They are essential tools in scripting and programming, providing a method to store and manipulate data. This article will take you through the fundamental assignment operators in Bash, along with examples of their usage.

Standard Assignment Operator

In Bash, the standard assignment operator is the `=` symbol. It is used to assign the value on the right-hand side to the variable on the left-hand side. There should not be any spaces around the `=` operator. Here is an example:

NAME="John Doe"

In this example, the variable `NAME` is assigned the value “John Doe”. If you use `echo $NAME`, the output will be “John Doe”.

Compound Assignment Operators

Compound assignment operators combine an operation and an assignment into a single operation.

  1. Addition Assignment Operator (+=): This operator adds the value on the right to the value of the variable on the left and assigns the result to the variable.
    VALUE=10
    VALUE+=5
    
    echo $VALUE   #Output: 15

    In the above example, the VALUE variable initially holds 10. VALUE+=5 increases VALUE by 5, so if you `echo VALUE`, the output will be 15.

  2. Subtraction Assignment Operator (-=): This operator subtracts the value on the right from the value of the variable on the left and assigns the result to the variable.
    VALUE=10
    VALUE-=5
    
    echo $VALUE   #Output: 5

    In the above example, the VALUE variable initially holds 10. VALUE-=5 reduces VALUE by 5, so if you `echo VALUE`, the output will be 5.

Please note that Bash only supports integer arithmetic natively. If you need to perform operations with floating-point numbers, you will need to use external tools like bc.

Read-only Assignment Operator

The readonly operator is used to make a variable’s value constant, which means the value assigned to the variable cannot be changed later. If you try to change the value of a readonly variable, Bash will give an error.

readonly PI=3.14
PI=3.1415

In the above example, PI is declared as a readonly variable and assigned a value of 3.14. When we try to reassign the value 3.1415 to PI, Bash will give an error message: bash: PI: readonly variable.

Local Assignment Operator

The local operator is used within functions to create a local variable – a variable that can only be accessed within the function where it was declared.

function my_func() {
    local MY_VAR="I am local"
    echo $MY_VAR
}

my_func
echo $MY_VAR

In the above example, MY_VAR is declared as a local variable in the my_func function. When we call the function, it prints “I am local”. However, when we try to echo MY_VAR outside of the function, it prints nothing because MY_VAR is not accessible outside my_func.

Conclusion

Bash assignment operators are a crucial part of shell scripting, enabling the storage and manipulation of data. By understanding and using these operators effectively, you can enhance the functionality and efficiency of your scripts. This article covered the basic assignment operator, compound assignment operators, and special assignment operators like readonly and local. Understanding how and when to use each operator is a key aspect of mastering Bash scripting.

The post Assignment Operators in Bash appeared first on TecAdmin.

Bash Tips & Tricks TecAdmin