In bash programming, assignment operators are like tools that let you give values to things. For example, you can tell the computer that “x equals 5” or “name equals tecadmin”. This way, whenever you talk about “x” or “name” later in your code, the computer knows exactly what you mean. It’s like labeling boxes in a storeroom so you know what’s inside each one without opening them.
In this article, we have split the assignment operator into four basic types. Let’s go over each type one by one with an example to make it easier to understand.
1. Standard Assignment Operator
In Bash programming, 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. Make sure there are no spaces around the `=` operator.
Here is an quick example:
NAME="tecadmin"
In this example, the variable `SHELL` is assigned the value “tecadmin”. If you use `echo $DOAMIN`, the output will be “tecadmin”.
2. Compound Assignment Operators
The compound assignment operators combination of performing some operation and then assign value to variable in a single operation. Basically it reduces line of code in your script and increase performance.
- 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.
- 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.
3. 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.
4. 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
Assignment operators are used a lot in programming. In shell scripting, they help with saving and changing data. By learning and using these operators well, you can make your scripts work better and faster. This article talked about the basic assignment operator, compound assignment operators, and special ones like readonly and local. Knowing how and when to use each type is important for getting really good for writing Bash scripts.