Declaring Variables with Attributes
Explore how to declare Bash variables using attributes such as integer, readonly, and environment export. Understand arithmetic operations, string concatenation, and variable scope to write clear and functional Bash scripts.
We'll cover the following...
Here are several examples of how to declare variables with attributes. First, let’s compare integer and string variables. We execute the following two commands in the terminal:
declare -i sum=11+2
text=11+2
Run the commands discussed in this lesson in the terminal below.
We declared two variables named sum and text. The sum variable has the integer attribute. Therefore, its value equals 13—that is, the sum of 11 and 2. The text variable is equal to the “11+2” string.
Bash stores both variables as strings in the memory. The -i option does not specify the variable’s type. Instead, it limits the allowed values of the variable.
Let’s try to assign a string to the sum variable. We can do it in one of ...